Mercurial > crates > nonstick
annotate src/libpam/conversation.rs @ 171:e27c5c667a5a
Create full new types for return code and flags, separate end to end.
This plumbs the ReturnCode and RawFlags types through the places where
we call into or are called from PAM.
Also adds Sun documentation to the project.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Fri, 25 Jul 2025 20:52:14 -0400 |
parents | a75a66cb4181 |
children | 6727cbe56f4a |
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}; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
2 use crate::conv::{Conversation, ErrorMsg, Exchange, InfoMsg, MaskedQAndA, QAndA}; |
87
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}; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
5 use crate::libpam::question::Question; |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 use crate::ErrorCode; |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
7 use crate::Result; |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
8 use libpam_sys::aliases::ConversationCallback; |
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
9 use libpam_sys_helpers::PtrPtrVec; |
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
10 use std::ffi::{c_int, c_void}; |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 use std::iter; |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
12 use std::ptr::NonNull; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
13 use std::result::Result as StdResult; |
171
e27c5c667a5a
Create full new types for return code and flags, separate end to end.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
14 use crate::constants::ReturnCode; |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
16 /// The type used by PAM to call back into a conversation. |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
17 /// |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
18 /// This has the same structure as a [`libpam_sys::pam_conv`]. |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
19 #[derive(Debug)] |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
20 #[repr(C)] |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
21 pub struct OwnedConversation<C: Conversation> { |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
22 callback: ConversationCallback, |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
23 conv: Box<C>, |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
24 } |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
25 |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
26 impl<C: Conversation> OwnedConversation<C> { |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
27 pub fn new(conv: C) -> Self { |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 Self { |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
29 callback: Self::wrapper_callback, |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
30 conv: Box::new(conv), |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
34 /// 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
|
35 /// |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
36 /// PAM calls this, we compute answers, then send them back. |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
37 unsafe extern "C" fn wrapper_callback( |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
38 count: c_int, |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
39 questions: *const *const libpam_sys::pam_message, |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
40 answers: *mut *mut libpam_sys::pam_response, |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
41 me: *mut c_void, |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 ) -> c_int { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
43 let internal = || { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
44 // Collect all our pointers |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 let conv = me |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 .cast::<C>() |
97
efe2f5f8b5b2
Implement "stateless" application-side PAM calls.
Paul Fisher <paul@pfish.zone>
parents:
96
diff
changeset
|
47 .as_ref() |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 .ok_or(ErrorCode::ConversationError)?; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
49 let q_iter = PtrPtrVec::<Question>::iter_over(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
|
50 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
|
51 |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
52 // Build our owned list of Q&As from the questions we've been asked |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
53 let messages: Vec<OwnedExchange> = q_iter |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
54 .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
|
55 .collect::<Result<_>>() |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 .map_err(|_| ErrorCode::ConversationError)?; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
57 // 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
|
58 // If we got an invalid message type, bail before sending. |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
59 let borrowed: Result<Vec<_>> = messages.iter().map(Exchange::try_from).collect(); |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
60 // 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
|
61 conv.communicate(&borrowed?); |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
62 |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
63 // Send our answers back. |
100
3f11b8d30f63
Implement environment variable management.
Paul Fisher <paul@pfish.zone>
parents:
98
diff
changeset
|
64 let owned = Answers::build(messages)?; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
65 *answers_ptr = owned.into_ptr(); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 Ok(()) |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 }; |
171
e27c5c667a5a
Create full new types for return code and flags, separate end to end.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
68 ReturnCode::from(internal()).into() |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
72 /// A conversation owned by a PAM handle and lent to us. |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
73 #[derive(Debug)] |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
74 pub struct PamConv(libpam_sys::pam_conv); |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
75 |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
76 impl Conversation for PamConv { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
77 fn communicate(&self, messages: &[Exchange]) { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
78 let internal = || { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
79 let questions: Result<_> = messages.iter().map(Question::try_from).collect(); |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
80 let questions = PtrPtrVec::new(questions?); |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
81 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
|
82 // 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
|
83 let result = unsafe { |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
84 (self.0.conv)( |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
85 messages.len() as c_int, |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
86 questions.as_ptr(), |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
87 &mut response_pointer, |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
88 self.0.appdata_ptr, |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
89 ) |
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 ErrorCode::result_from(result)?; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
92 // 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
|
93 // 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
|
94 // 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
|
95 unsafe { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
96 let response_pointer = |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
97
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 convert(msg, response); |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
101 } |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
102 }; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
103 Ok(()) |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
104 }; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
105 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
|
106 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
|
107 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
109 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
110 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
111 /// Like [`Exchange`], 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
|
112 #[derive(Debug)] |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
113 pub enum OwnedExchange<'a> { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
114 MaskedPrompt(MaskedQAndA<'a>), |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
115 Prompt(QAndA<'a>), |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
116 Info(InfoMsg<'a>), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
117 Error(ErrorMsg<'a>), |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
118 RadioPrompt(RadioQAndA<'a>), |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
119 BinaryPrompt(BinaryQAndA<'a>), |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
120 } |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
121 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
122 impl<'a> TryFrom<&'a OwnedExchange<'a>> for Exchange<'a> { |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
80
diff
changeset
|
123 type Error = ErrorCode; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
124 fn try_from(src: &'a OwnedExchange) -> StdResult<Self, ErrorCode> { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
125 match src { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
126 OwnedExchange::MaskedPrompt(m) => Ok(Exchange::MaskedPrompt(m)), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
127 OwnedExchange::Prompt(m) => Ok(Exchange::Prompt(m)), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
128 OwnedExchange::Info(m) => Ok(Exchange::Info(m)), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
129 OwnedExchange::Error(m) => Ok(Exchange::Error(m)), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
130 OwnedExchange::RadioPrompt(m) => Ok(Exchange::RadioPrompt(m)), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
131 OwnedExchange::BinaryPrompt(m) => Ok(Exchange::BinaryPrompt(m)), |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
132 } |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
133 } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
134 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
135 |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
136 /// 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
|
137 /// |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
138 /// # Safety |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
139 /// |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
140 /// You are responsible for ensuring that the src-dst pair matches. |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
141 unsafe fn convert(msg: &Exchange, resp: &mut Answer) { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
142 macro_rules! fill_text { |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
143 ($dst:ident, $src:ident) => {{ |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
144 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
|
145 $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
|
146 }}; |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
147 } |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
148 match *msg { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
149 Exchange::MaskedPrompt(qa) => fill_text!(qa, resp), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
150 Exchange::Prompt(qa) => fill_text!(qa, resp), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
151 Exchange::Error(m) => m.set_answer(Ok(())), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
152 Exchange::Info(m) => m.set_answer(Ok(())), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
153 Exchange::RadioPrompt(qa) => fill_text!(qa, resp), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
101
diff
changeset
|
154 Exchange::BinaryPrompt(qa) => { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
155 let bin_resp = unsafe { BinaryAnswer::upcast(resp) }; |
93
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
156 qa.set_answer(Ok(bin_resp |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
157 .contents() |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
136
diff
changeset
|
158 .map(|d| d.into()) |
93
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
159 .unwrap_or_default())); |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
160 bin_resp.zero_contents() |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
161 } |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
162 } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
163 } |