Mercurial > crates > nonstick
annotate src/libpam/conversation.rs @ 100:3f11b8d30f63
Implement environment variable management.
This actually wires up the environment variable handling to libpam,
so that applications and modules can manage the environment through
the authentication process.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Tue, 24 Jun 2025 17:08:01 -0400 |
| parents | b87100c5eed4 |
| children | 94b51fa4f797 |
| rev | line source |
|---|---|
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
1 use crate::conv::{BinaryQAndA, RadioQAndA}; |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
2 use crate::conv::{Conversation, ErrorMsg, InfoMsg, MaskedQAndA, Message, QAndA}; |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
3 use crate::libpam::answer::BinaryAnswer; |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
4 use crate::libpam::answer::{Answer, Answers, TextAnswer}; |
|
93
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
5 use crate::libpam::memory::{CBinaryData, Immovable}; |
|
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
78
diff
changeset
|
6 use crate::libpam::pam_ffi::AppData; |
|
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
78
diff
changeset
|
7 pub use crate::libpam::pam_ffi::LibPamConversation; |
|
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
8 use crate::libpam::question::QuestionsTrait; |
|
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
9 use crate::libpam::question::{Question, Questions}; |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 use crate::ErrorCode; |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
11 use crate::Result; |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 use std::ffi::c_int; |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 use std::iter; |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 use std::marker::PhantomData; |
|
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
15 use std::ptr::NonNull; |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
16 use std::result::Result as StdResult; |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 impl LibPamConversation<'_> { |
|
97
efe2f5f8b5b2
Implement "stateless" application-side PAM calls.
Paul Fisher <paul@pfish.zone>
parents:
96
diff
changeset
|
19 pub fn wrap<C: Conversation>(conv: &C) -> Self { |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 Self { |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 callback: Self::wrapper_callback::<C>, |
|
97
efe2f5f8b5b2
Implement "stateless" application-side PAM calls.
Paul Fisher <paul@pfish.zone>
parents:
96
diff
changeset
|
22 appdata: (conv as *const C).cast(), |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 life: PhantomData, |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 _marker: Immovable(PhantomData), |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
28 /// Passed as the conversation function into PAM for an owned handle. |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
29 /// |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
30 /// PAM calls this, we compute answers, then send them back. |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 unsafe extern "C" fn wrapper_callback<C: Conversation>( |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 count: c_int, |
|
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
78
diff
changeset
|
33 questions: *const *const Question, |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
34 answers: *mut *mut Answer, |
|
97
efe2f5f8b5b2
Implement "stateless" application-side PAM calls.
Paul Fisher <paul@pfish.zone>
parents:
96
diff
changeset
|
35 me: *const AppData, |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 ) -> c_int { |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
37 let internal = || { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
38 // Collect all our pointers |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
39 let conv = me |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 .cast::<C>() |
|
97
efe2f5f8b5b2
Implement "stateless" application-side PAM calls.
Paul Fisher <paul@pfish.zone>
parents:
96
diff
changeset
|
41 .as_ref() |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 .ok_or(ErrorCode::ConversationError)?; |
|
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
43 let indirect = Questions::borrow_ptr(questions, count as usize); |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
44 let answers_ptr = answers.as_mut().ok_or(ErrorCode::ConversationError)?; |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
45 |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
46 // Build our owned list of Q&As from the questions we've been asked |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
47 let messages: Vec<OwnedMessage> = indirect |
|
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
48 .map(TryInto::try_into) |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
49 .collect::<Result<_>>() |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 .map_err(|_| ErrorCode::ConversationError)?; |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
51 // Borrow all those Q&As and ask them. |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
52 // If we got an invalid message type, bail before sending. |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
53 let borrowed: Result<Vec<_>> = messages.iter().map(Message::try_from).collect(); |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
54 // TODO: Do we want to log something here? |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
55 conv.communicate(&borrowed?); |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
56 |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
57 // Send our answers back. |
|
100
3f11b8d30f63
Implement environment variable management.
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
58 let owned = Answers::build(messages)?; |
|
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
59 *answers_ptr = owned.into_ptr().as_ptr(); |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
60 Ok(()) |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 }; |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
62 ErrorCode::result_to_c(internal()) |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 impl Conversation for LibPamConversation<'_> { |
|
96
f3e260f9ddcb
Make conversation trait use immutable references.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
67 fn communicate(&self, messages: &[Message]) { |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
68 let internal = || { |
|
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
69 let questions = Questions::new(messages)?; |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
70 let mut response_pointer = std::ptr::null_mut(); |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
71 // SAFETY: We're calling into PAM with valid everything. |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
72 let result = unsafe { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
73 (self.callback)( |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
74 messages.len() as c_int, |
|
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
75 questions.ptr(), |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
76 &mut response_pointer, |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
77 self.appdata, |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
78 ) |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
79 }; |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
80 ErrorCode::result_from(result)?; |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
81 // SAFETY: This is a pointer we just got back from PAM. |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
82 // We have to trust that the responses from PAM match up |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
83 // with the questions we sent. |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
84 unsafe { |
|
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
85 let response_pointer = |
|
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
86 NonNull::new(response_pointer).ok_or(ErrorCode::ConversationError)?; |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
87 let mut owned_responses = Answers::from_c_heap(response_pointer, messages.len()); |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
88 for (msg, response) in iter::zip(messages, owned_responses.iter_mut()) { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
89 convert(msg, response); |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
90 } |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
91 }; |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
92 Ok(()) |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
93 }; |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
94 if let Err(e) = internal() { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
95 messages.iter().for_each(|m| m.set_error(e)) |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
100 /// Like [`Message`], but this time we own the contents. |
|
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
101 #[derive(Debug)] |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
102 pub enum OwnedMessage<'a> { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
103 MaskedPrompt(MaskedQAndA<'a>), |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
104 Prompt(QAndA<'a>), |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
105 Info(InfoMsg<'a>), |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
106 Error(ErrorMsg<'a>), |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
107 RadioPrompt(RadioQAndA<'a>), |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
108 BinaryPrompt(BinaryQAndA<'a>), |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
109 } |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
110 |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
111 impl<'a> TryFrom<&'a OwnedMessage<'a>> for Message<'a> { |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
112 type Error = ErrorCode; |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
113 fn try_from(src: &'a OwnedMessage) -> StdResult<Self, ErrorCode> { |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
114 match src { |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
115 OwnedMessage::MaskedPrompt(m) => Ok(Message::MaskedPrompt(m)), |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
116 OwnedMessage::Prompt(m) => Ok(Message::Prompt(m)), |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
117 OwnedMessage::Info(m) => Ok(Message::Info(m)), |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
118 OwnedMessage::Error(m) => Ok(Message::Error(m)), |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
119 OwnedMessage::RadioPrompt(m) => Ok(Message::RadioPrompt(m)), |
|
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
120 OwnedMessage::BinaryPrompt(m) => Ok(Message::BinaryPrompt(m)), |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
121 } |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
122 } |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
123 } |
|
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
124 |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
125 /// Fills in the answer of the Message with the given response. |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
126 /// |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
127 /// # Safety |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
128 /// |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
129 /// You are responsible for ensuring that the src-dst pair matches. |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
130 unsafe fn convert(msg: &Message, resp: &mut Answer) { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
131 macro_rules! fill_text { |
|
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
132 ($dst:ident, $src:ident) => {{ |
|
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
133 let text_resp = unsafe { TextAnswer::upcast($src) }; |
|
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
134 $dst.set_answer(text_resp.contents().map(Into::into)); |
|
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
135 }}; |
|
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
136 } |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
137 match *msg { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
138 Message::MaskedPrompt(qa) => fill_text!(qa, resp), |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
139 Message::Prompt(qa) => fill_text!(qa, resp), |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
140 Message::Error(m) => m.set_answer(Ok(())), |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
141 Message::Info(m) => m.set_answer(Ok(())), |
|
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
142 Message::RadioPrompt(qa) => fill_text!(qa, resp), |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
143 Message::BinaryPrompt(qa) => { |
|
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
144 let bin_resp = unsafe { BinaryAnswer::upcast(resp) }; |
|
93
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
145 qa.set_answer(Ok(bin_resp |
|
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
146 .data() |
|
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
147 .map(|d| unsafe { CBinaryData::as_binary_data(d) }) |
|
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
148 .unwrap_or_default())); |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
149 bin_resp.zero_contents() |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
150 } |
|
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
151 } |
|
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
152 } |
