Mercurial > crates > nonstick
annotate src/libpam/question.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 | 49d9e2b5c189 |
rev | line source |
---|---|
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
1 //! Data and types dealing with PAM messages. |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
2 |
105 | 3 use std::cell::Cell; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
4 #[cfg(feature = "linux-pam-extensions")] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
5 use crate::conv::{BinaryQAndA, RadioQAndA}; |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
6 use crate::conv::{ErrorMsg, InfoMsg, MaskedQAndA, Message, QAndA}; |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
7 use crate::libpam::conversation::OwnedMessage; |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
8 use crate::libpam::memory::{CBinaryData, CHeapBox, CHeapString, Immovable}; |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
9 use crate::libpam::pam_ffi; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
10 pub use crate::libpam::pam_ffi::Question; |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
11 use crate::ErrorCode; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
12 use crate::Result; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
13 use num_enum::{IntoPrimitive, TryFromPrimitive}; |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
14 use std::ffi::{c_void, CStr}; |
101 | 15 use std::pin::Pin; |
105 | 16 use std::{ptr, slice}; |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
17 |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
18 /// Abstraction of a collection of questions to be sent in a PAM conversation. |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
19 /// |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
20 /// The PAM C API conversation function looks like this: |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
21 /// |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
22 /// ```c |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
23 /// int pam_conv( |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
24 /// int count, |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
25 /// const struct pam_message **questions, |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
26 /// struct pam_response **answers, |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
27 /// void *appdata_ptr, |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
28 /// ) |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
29 /// ``` |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
30 /// |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
31 /// On Linux-PAM and other compatible implementations, `questions` |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
32 /// is treated as a pointer-to-pointers, like `int argc, char **argv`. |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
33 /// (In this situation, the value of `Questions.indirect` is |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
34 /// the pointer passed to `pam_conv`.) |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
35 /// |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
36 /// ```text |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
37 /// points to ┌───────────────┐ ╔═ Question ═╗ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
38 /// questions ┄┄┄┄┄┄┄┄┄┄> │ questions[0] ┄┼┄┄┄┄> ║ style ║ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
39 /// │ questions[1] ┄┼┄┄┄╮ ║ data ┄┄┄┄┄┄╫┄┄> ... |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
40 /// │ ... │ ┆ ╚════════════╝ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
41 /// ┆ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
42 /// ┆ ╔═ Question ═╗ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
43 /// ╰┄┄> ║ style ║ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
44 /// ║ data ┄┄┄┄┄┄╫┄┄> ... |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
45 /// ╚════════════╝ |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
46 /// ``` |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
47 /// |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
48 /// On OpenPAM and other compatible implementations (like Solaris), |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
49 /// `messages` is a pointer-to-pointer-to-array. This appears to be |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
50 /// the correct implementation as required by the XSSO specification. |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
51 /// |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
52 /// ```text |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
53 /// points to ┌─────────────┐ ╔═ Question[] ═╗ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
54 /// questions ┄┄┄┄┄┄┄┄┄┄> │ *questions ┄┼┄┄┄┄┄> ║ style ║ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
55 /// └─────────────┘ ║ data ┄┄┄┄┄┄┄┄╫┄┄> ... |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
56 /// ╟──────────────╢ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
57 /// ║ style ║ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
58 /// ║ data ┄┄┄┄┄┄┄┄╫┄┄> ... |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
59 /// ╟──────────────╢ |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
60 /// ║ ... ║ |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
61 /// ``` |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
62 pub trait QuestionsTrait { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
63 /// Allocates memory for this indirector and all its members. |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
64 fn new(messages: &[Message]) -> Result<Self> |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
65 where |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
66 Self: Sized; |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
67 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
68 /// Gets the pointer that is passed . |
101 | 69 fn ptr(self: Pin<&Self>) -> *const *const Question; |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
70 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
71 /// Converts a pointer into a borrowed list of Questions. |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
72 /// |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
73 /// # Safety |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
74 /// |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
75 /// You have to provide a valid pointer. |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
76 unsafe fn borrow_ptr<'a>( |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
77 ptr: *const *const Question, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
78 count: usize, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
79 ) -> impl Iterator<Item = &'a Question>; |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
80 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
81 |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
82 #[cfg(pam_impl = "linux-pam")] |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
83 pub type Questions = LinuxPamQuestions; |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
84 |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
85 #[cfg(not(pam_impl = "linux-pam"))] |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
86 pub type Questions = XSsoQuestions; |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
87 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
88 /// The XSSO standard version of the pointer train to questions. |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
89 #[derive(Debug)] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
90 #[repr(C)] |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
91 pub struct XSsoQuestions { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
92 /// Points to the memory address where the meat of `questions` is. |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
93 /// **The memory layout of Vec is not specified**, and we need to return |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
94 /// a pointer to the pointer, hence we have to store it here. |
105 | 95 pointer: Cell<*const Question>, |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
96 questions: Vec<Question>, |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
97 _marker: Immovable, |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
98 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
99 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
100 impl XSsoQuestions { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
101 fn len(&self) -> usize { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
102 self.questions.len() |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
103 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
104 fn iter_mut(&mut self) -> impl Iterator<Item = &mut Question> { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
105 self.questions.iter_mut() |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
106 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
107 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
108 |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
109 impl QuestionsTrait for XSsoQuestions { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
110 fn new(messages: &[Message]) -> Result<Self> { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
111 let questions: Result<Vec<_>> = messages.iter().map(Question::try_from).collect(); |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
112 let questions = questions?; |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
113 Ok(Self { |
105 | 114 pointer: Cell::new(ptr::null()), |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
115 questions, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
116 _marker: Default::default(), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
117 }) |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
118 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
119 |
101 | 120 fn ptr(self: Pin<&Self>) -> *const *const Question { |
105 | 121 let me = self.get_ref(); |
122 me.pointer.set(self.questions.as_ptr()); | |
123 me.pointer.as_ptr() | |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
124 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
125 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
126 unsafe fn borrow_ptr<'a>( |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
127 ptr: *const *const Question, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
128 count: usize, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
129 ) -> impl Iterator<Item = &'a Question> { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
130 slice::from_raw_parts(*ptr, count).iter() |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
131 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
132 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
133 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
134 /// The Linux version of the pointer train to questions. |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
135 #[derive(Debug)] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
136 #[repr(C)] |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
137 pub struct LinuxPamQuestions { |
101 | 138 #[allow(clippy::vec_box)] // we need to box vec items. |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
139 /// The place where the questions are. |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
140 questions: Vec<Box<Question>>, |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
141 } |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
142 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
143 impl LinuxPamQuestions { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
144 fn len(&self) -> usize { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
145 self.questions.len() |
78
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 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
148 fn iter_mut(&mut self) -> impl Iterator<Item = &mut Question> { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
149 self.questions.iter_mut().map(AsMut::as_mut) |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
150 } |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
151 } |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
152 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
153 impl QuestionsTrait for LinuxPamQuestions { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
154 fn new(messages: &[Message]) -> Result<Self> { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
155 let questions: Result<_> = messages |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
156 .iter() |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
157 .map(|msg| Question::try_from(msg).map(Box::new)) |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
158 .collect(); |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
159 Ok(Self { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
160 questions: questions?, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
161 }) |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
162 } |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
163 |
101 | 164 fn ptr(self: Pin<&Self>) -> *const *const Question { |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
165 self.questions.as_ptr().cast() |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
166 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
167 |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
168 unsafe fn borrow_ptr<'a>( |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
169 ptr: *const *const Question, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
170 count: usize, |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
171 ) -> impl Iterator<Item = &'a Question> { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
172 slice::from_raw_parts(ptr.cast::<&Question>(), count) |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
173 .iter() |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
174 .copied() |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
175 } |
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
176 } |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
177 |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
178 /// The C enum values for messages shown to the user. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
179 #[derive(Debug, PartialEq, TryFromPrimitive, IntoPrimitive)] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
180 #[repr(u32)] |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
181 enum Style { |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
182 /// Requests information from the user; will be masked when typing. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
183 PromptEchoOff = pam_ffi::PAM_PROMPT_ECHO_OFF, |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
184 /// Requests information from the user; will not be masked. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
185 PromptEchoOn = pam_ffi::PAM_PROMPT_ECHO_ON, |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
186 /// An error message. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
187 ErrorMsg = pam_ffi::PAM_ERROR_MSG, |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
188 /// An informational message. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
189 TextInfo = pam_ffi::PAM_TEXT_INFO, |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
190 /// Yes/No/Maybe conditionals. A Linux-PAM extension. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
191 #[cfg(feature = "linux-pam-extensions")] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
192 RadioType = pam_ffi::PAM_RADIO_TYPE, |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
193 /// For server–client non-human interaction. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
194 /// |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
195 /// NOT part of the X/Open PAM specification. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
196 /// A Linux-PAM extension. |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
197 #[cfg(feature = "linux-pam-extensions")] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
198 BinaryPrompt = pam_ffi::PAM_BINARY_PROMPT, |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
199 } |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
200 |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
201 impl Question { |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
202 /// Gets this message's data pointer as a string. |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
203 /// |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
204 /// # Safety |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
205 /// |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
206 /// It's up to you to pass this only on types with a string value. |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
207 unsafe fn string_data(&self) -> Result<&str> { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
208 match self.data.as_ref() { |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
209 None => Ok(""), |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
210 Some(data) => CStr::from_ptr(CHeapBox::as_ptr(data).cast().as_ptr()) |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
211 .to_str() |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
212 .map_err(|_| ErrorCode::ConversationError), |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
213 } |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
214 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
215 |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
216 /// Gets this message's data pointer as borrowed binary data. |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
217 unsafe fn binary_data(&self) -> (&[u8], u8) { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
218 self.data |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
219 .as_ref() |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
220 .map(|data| CBinaryData::data(CHeapBox::as_ptr(data).cast())) |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
221 .unwrap_or_default() |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
222 } |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
223 } |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
224 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
225 impl TryFrom<&Message<'_>> for Question { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
226 type Error = ErrorCode; |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
227 fn try_from(msg: &Message) -> Result<Self> { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
228 let alloc = |style, text| -> Result<_> { |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
229 Ok((style, unsafe { |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
230 CHeapBox::cast(CHeapString::new(text)?.into_box()) |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
231 })) |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
232 }; |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
233 // We will only allocate heap data if we have a valid input. |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
234 let (style, data): (_, CHeapBox<c_void>) = match *msg { |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
235 Message::MaskedPrompt(p) => alloc(Style::PromptEchoOff, p.question()), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
236 Message::Prompt(p) => alloc(Style::PromptEchoOn, p.question()), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
237 Message::Error(p) => alloc(Style::ErrorMsg, p.question()), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
238 Message::Info(p) => alloc(Style::TextInfo, p.question()), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
239 #[cfg(feature = "linux-pam-extensions")] |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
240 Message::RadioPrompt(p) => alloc(Style::RadioType, p.question()), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
241 #[cfg(feature = "linux-pam-extensions")] |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
242 Message::BinaryPrompt(p) => Ok((Style::BinaryPrompt, unsafe { |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
243 CHeapBox::cast(CBinaryData::alloc(p.question())?) |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
244 })), |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
245 #[cfg(not(feature = "linux-pam-extensions"))] |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
246 Message::RadioPrompt(_) | Message::BinaryPrompt(_) => Err(ErrorCode::ConversationError), |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
247 }?; |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
248 Ok(Self { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
249 style: style.into(), |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
250 data: Some(data), |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
251 }) |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
252 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
253 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
254 |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
255 impl Drop for Question { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
256 fn drop(&mut self) { |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
257 // SAFETY: We either created this data or we got it from PAM. |
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
258 // After this function is done, it will be zeroed out. |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
259 unsafe { |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
260 // This is nice-to-have. We'll try to zero out the data |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
261 // in the Question. If it's not a supported format, we skip it. |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
262 if let Ok(style) = Style::try_from(self.style) { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
263 let _ = match style { |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
264 #[cfg(feature = "linux-pam-extensions")] |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
265 Style::BinaryPrompt => self |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
266 .data |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
267 .as_ref() |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
268 .map(|p| CBinaryData::zero_contents(CHeapBox::as_ptr(p).cast())), |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
269 #[cfg(feature = "linux-pam-extensions")] |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
270 Style::RadioType => self |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
271 .data |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
272 .as_ref() |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
273 .map(|p| CHeapString::zero(CHeapBox::as_ptr(p).cast())), |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
274 Style::TextInfo |
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
275 | Style::ErrorMsg |
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
276 | Style::PromptEchoOff |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
277 | Style::PromptEchoOn => self |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
278 .data |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
279 .as_ref() |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
280 .map(|p| CHeapString::zero(CHeapBox::as_ptr(p).cast())), |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
281 }; |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
282 }; |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
283 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
284 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
285 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
286 |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
287 impl<'a> TryFrom<&'a Question> for OwnedMessage<'a> { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
288 type Error = ErrorCode; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
289 fn try_from(question: &'a Question) -> Result<Self> { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
290 let style: Style = question |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
291 .style |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
292 .try_into() |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
293 .map_err(|_| ErrorCode::ConversationError)?; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
294 // SAFETY: In all cases below, we're creating questions based on |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
295 // known types that we get from PAM and the inner types it should have. |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
296 let prompt = unsafe { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
297 match style { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
298 Style::PromptEchoOff => { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
299 Self::MaskedPrompt(MaskedQAndA::new(question.string_data()?)) |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
300 } |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
301 Style::PromptEchoOn => Self::Prompt(QAndA::new(question.string_data()?)), |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
302 Style::ErrorMsg => Self::Error(ErrorMsg::new(question.string_data()?)), |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
303 Style::TextInfo => Self::Info(InfoMsg::new(question.string_data()?)), |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
304 #[cfg(feature = "linux-pam-extensions")] |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
305 Style::RadioType => Self::RadioPrompt(RadioQAndA::new(question.string_data()?)), |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
306 #[cfg(feature = "linux-pam-extensions")] |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
307 Style::BinaryPrompt => Self::BinaryPrompt(BinaryQAndA::new(question.binary_data())), |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
308 } |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
309 }; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
310 Ok(prompt) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
311 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
312 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
313 |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
314 #[cfg(test)] |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
315 mod tests { |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
316 |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
317 macro_rules! assert_matches { |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
318 ($id:ident => $variant:path, $q:expr) => { |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
319 if let $variant($id) = $id { |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
320 assert_eq!($q, $id.question()); |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
321 } else { |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
322 panic!("mismatched enum variant {x:?}", x = $id); |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
323 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
324 }; |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
325 } |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
326 |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
327 macro_rules! tests { ($fn_name:ident<$typ:ident>) => { |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
328 mod $fn_name { |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
329 use super::super::*; |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
330 #[test] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
331 fn standard() { |
101 | 332 let interrogation = Box::pin(<$typ>::new(&[ |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
333 MaskedQAndA::new("hocus pocus").message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
334 QAndA::new("what").message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
335 QAndA::new("who").message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
336 InfoMsg::new("hey").message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
337 ErrorMsg::new("gasp").message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
338 ]) |
101 | 339 .unwrap()); |
340 let indirect = interrogation.as_ref().ptr(); | |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
341 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
342 let remade = unsafe { $typ::borrow_ptr(indirect, interrogation.len()) }; |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
343 let messages: Vec<OwnedMessage> = remade |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
344 .map(TryInto::try_into) |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
345 .collect::<Result<_>>() |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
346 .unwrap(); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
347 let [masked, what, who, hey, gasp] = messages.try_into().unwrap(); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
348 assert_matches!(masked => OwnedMessage::MaskedPrompt, "hocus pocus"); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
349 assert_matches!(what => OwnedMessage::Prompt, "what"); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
350 assert_matches!(who => OwnedMessage::Prompt, "who"); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
351 assert_matches!(hey => OwnedMessage::Info, "hey"); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
352 assert_matches!(gasp => OwnedMessage::Error, "gasp"); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
353 } |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
354 |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
355 #[test] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
356 #[cfg(not(feature = "linux-pam-extensions"))] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
357 fn no_linux_extensions() { |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
358 use crate::conv::{BinaryQAndA, RadioQAndA}; |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
359 <$typ>::new(&[ |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
360 BinaryQAndA::new((&[5, 4, 3, 2, 1], 66)).message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
361 RadioQAndA::new("you must choose").message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
362 ]).unwrap_err(); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
363 } |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
364 |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
365 #[test] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
366 #[cfg(feature = "linux-pam-extensions")] |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
367 fn linux_extensions() { |
101 | 368 let interrogation = Box::pin(<$typ>::new(&[ |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
369 BinaryQAndA::new((&[5, 4, 3, 2, 1], 66)).message(), |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
370 RadioQAndA::new("you must choose").message(), |
101 | 371 ]).unwrap()); |
372 let indirect = interrogation.as_ref().ptr(); | |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
373 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
374 let remade = unsafe { $typ::borrow_ptr(indirect, interrogation.len()) }; |
93
efc2b56c8928
Remove undefined behavior per MIRI.
Paul Fisher <paul@pfish.zone>
parents:
89
diff
changeset
|
375 let messages: Vec<OwnedMessage> = remade |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
376 .map(TryInto::try_into) |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
377 .collect::<Result<_>>() |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
378 .unwrap(); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
379 let [bin, choose] = messages.try_into().unwrap(); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
380 assert_matches!(bin => OwnedMessage::BinaryPrompt, (&[5, 4, 3, 2, 1][..], 66)); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
381 assert_matches!(choose => OwnedMessage::RadioPrompt, "you must choose"); |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
382 } |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
383 } |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
384 }} |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
385 |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
386 tests!(test_xsso<XSsoQuestions>); |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
387 tests!(test_linux<LinuxPamQuestions>); |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
388 } |