annotate src/libpam/question.rs @ 89:dd3e9c4bcde3

Simplify memory management in Questions. When we're sending Questions to the client, we don't need them to be C-managed, we just need the pointers going to the right place. This replaces a bunch of Question management cruft with Vecs and Boxes.
author Paul Fisher <paul@pfish.zone>
date Fri, 13 Jun 2025 05:22:48 -0400
parents 05291b601f0a
children efc2b56c8928
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
3 #[cfg(feature = "linux-pam-extensions")]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
4 use crate::conv::{BinaryQAndA, RadioQAndA};
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
5 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
6 use crate::libpam::conversation::OwnedMessage;
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
7 use crate::libpam::memory::{CBinaryData, Immovable};
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
8 pub use crate::libpam::pam_ffi::Question;
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
9 use crate::libpam::{memory, pam_ffi};
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
10 use crate::ErrorCode;
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
11 use crate::Result;
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
12 use num_enum::{IntoPrimitive, TryFromPrimitive};
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
13 use std::ffi::{c_void, CStr};
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
14 use std::{ptr, slice};
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
15
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
16 /// 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
17 ///
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
18 /// 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
19 ///
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
20 /// ```c
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
21 /// int pam_conv(
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
22 /// int count,
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
23 /// const struct pam_message **questions,
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
24 /// struct pam_response **answers,
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
25 /// void *appdata_ptr,
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
26 /// )
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
27 /// ```
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 /// 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
30 /// 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
31 /// (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
32 /// the pointer passed to `pam_conv`.)
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
33 ///
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
34 /// ```text
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
35 /// points to ┌───────────────┐ ╔═ Question ═╗
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
36 /// questions ┄┄┄┄┄┄┄┄┄┄> │ questions[0] ┄┼┄┄┄┄> ║ style ║
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
37 /// │ questions[1] ┄┼┄┄┄╮ ║ data ┄┄┄┄┄┄╫┄┄> ...
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
38 /// │ ... │ ┆ ╚════════════╝
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
39 /// ┆
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
40 /// ┆ ╔═ Question ═╗
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
41 /// ╰┄┄> ║ style ║
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
42 /// ║ data ┄┄┄┄┄┄╫┄┄> ...
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
43 /// ╚════════════╝
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
44 /// ```
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
45 ///
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
46 /// 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
47 /// `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
48 /// 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
49 ///
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
50 /// ```text
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
51 /// points to ┌─────────────┐ ╔═ Question[] ═╗
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
52 /// questions ┄┄┄┄┄┄┄┄┄┄> │ *questions ┄┼┄┄┄┄┄> ║ style ║
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
53 /// └─────────────┘ ║ data ┄┄┄┄┄┄┄┄╫┄┄> ...
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
54 /// ╟──────────────╢
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
55 /// ║ style ║
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
56 /// ║ data ┄┄┄┄┄┄┄┄╫┄┄> ...
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
57 /// ╟──────────────╢
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
58 /// ║ ... ║
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
59 /// ```
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
60 pub trait QuestionsTrait {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
61 /// Allocates memory for this indirector and all its members.
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
62 fn new(messages: &[Message]) -> Result<Self>
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
63 where
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
64 Self: Sized;
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
65
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
66 /// Gets the pointer that is passed .
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
67 fn ptr(&self) -> *const *const Question;
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
68
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
69 /// 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
70 ///
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
71 /// # Safety
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 /// You have to provide a valid pointer.
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
74 unsafe fn borrow_ptr<'a>(
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
75 ptr: *const *const Question,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
76 count: usize,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
77 ) -> impl Iterator<Item = &'a Question>;
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
78 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
79
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
80 #[cfg(pam_impl = "linux-pam")]
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
81 pub type Questions = LinuxPamQuestions;
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
82
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
83 #[cfg(not(pam_impl = "linux-pam"))]
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
84 pub type Questions = XSsoQuestions;
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
85
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
86 /// 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
87 #[derive(Debug)]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
88 #[repr(C)]
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
89 pub struct XSsoQuestions {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
90 /// 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
91 /// **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
92 /// a pointer to the pointer, hence we have to store it here.
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
93 pointer: *const Question,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
94 questions: Vec<Question>,
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
95 _marker: Immovable,
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
96 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
97
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
98 impl XSsoQuestions {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
99 fn len(&self) -> usize {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
100 self.questions.len()
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
101 }
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
102 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
103 self.questions.iter_mut()
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
104 }
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
105 }
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 impl QuestionsTrait for XSsoQuestions {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
108 fn new(messages: &[Message]) -> Result<Self> {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
109 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
110 let questions = questions?;
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
111 Ok(Self {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
112 pointer: questions.as_ptr(),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
113 questions,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
114 _marker: Default::default(),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
115 })
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
116 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
117
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
118 fn ptr(&self) -> *const *const Question {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
119 &self.pointer as *const *const Question
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
120 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
121
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
122 unsafe fn borrow_ptr<'a>(
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
123 ptr: *const *const Question,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
124 count: usize,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
125 ) -> impl Iterator<Item = &'a Question> {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
126 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
127 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
128 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
129
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
130 /// 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
131 #[derive(Debug)]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
132 #[repr(C)]
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
133 pub struct LinuxPamQuestions {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
134 #[allow(clippy::vec_box)] // we need to do this.
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
135 /// The place where the questions are.
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
136 questions: Vec<Box<Question>>,
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
137 _marker: Immovable,
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
138 }
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
139
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
140 impl LinuxPamQuestions {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
141 fn len(&self) -> usize {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
142 self.questions.len()
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
143 }
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
144
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
145 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
146 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
147 }
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
148 }
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
149
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
150 impl QuestionsTrait for LinuxPamQuestions {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
151 fn new(messages: &[Message]) -> Result<Self> {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
152 let questions: Result<_> = messages
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
153 .iter()
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
154 .map(|msg| Question::try_from(msg).map(Box::new))
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
155 .collect();
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
156 Ok(Self {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
157 questions: questions?,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
158 _marker: Default::default(),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
159 })
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
160 }
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
161
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
162 fn ptr(&self) -> *const *const Question {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
163 self.questions.as_ptr().cast()
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
164 }
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
165
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
166 unsafe fn borrow_ptr<'a>(
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
167 ptr: *const *const Question,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
168 count: usize,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
169 ) -> impl Iterator<Item = &'a Question> {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
170 slice::from_raw_parts(ptr.cast::<&Question>(), count)
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
171 .iter()
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
172 .copied()
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
173 }
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
174 }
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
175
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
176 /// 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
177 #[derive(Debug, PartialEq, TryFromPrimitive, IntoPrimitive)]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
178 #[repr(u32)]
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
179 enum Style {
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
180 /// 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
181 PromptEchoOff = pam_ffi::PAM_PROMPT_ECHO_OFF,
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
182 /// 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
183 PromptEchoOn = pam_ffi::PAM_PROMPT_ECHO_ON,
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
184 /// An error message.
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
185 ErrorMsg = pam_ffi::PAM_ERROR_MSG,
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
186 /// An informational message.
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
187 TextInfo = pam_ffi::PAM_TEXT_INFO,
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
188 /// 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
189 #[cfg(feature = "linux-pam-extensions")]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
190 RadioType = pam_ffi::PAM_RADIO_TYPE,
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
191 /// For server–client non-human interaction.
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
192 ///
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
193 /// 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
194 /// A Linux-PAM extension.
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
195 #[cfg(feature = "linux-pam-extensions")]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
196 BinaryPrompt = pam_ffi::PAM_BINARY_PROMPT,
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
197 }
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
198
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
199 impl Question {
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
200 /// 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
201 ///
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
202 /// # Safety
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
203 ///
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
204 /// 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
205 unsafe fn string_data(&self) -> Result<&str> {
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
206 if self.data.is_null() {
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
207 Ok("")
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
208 } else {
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
209 CStr::from_ptr(self.data.cast())
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
210 .to_str()
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
211 .map_err(|_| ErrorCode::ConversationError)
70
9f8381a1c09c Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents: 69
diff changeset
212 }
69
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
213 }
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
214
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
215 /// 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
216 unsafe fn binary_data(&self) -> (&[u8], u8) {
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
217 self.data
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
218 .cast::<CBinaryData>()
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
219 .as_ref()
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
220 .map(Into::into)
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> {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
228 let alloc = |style, text| Ok((style, memory::malloc_str(text)?.cast()));
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
229 // We will only allocate heap data if we have a valid input.
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
230 let (style, data): (_, *mut c_void) = match *msg {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
231 Message::MaskedPrompt(p) => alloc(Style::PromptEchoOff, p.question()),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
232 Message::Prompt(p) => alloc(Style::PromptEchoOn, p.question()),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
233 Message::Error(p) => alloc(Style::ErrorMsg, p.question()),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
234 Message::Info(p) => alloc(Style::TextInfo, p.question()),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
235 #[cfg(feature = "linux-pam-extensions")]
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
236 Message::RadioPrompt(p) => alloc(Style::RadioType, p.question()),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
237 #[cfg(feature = "linux-pam-extensions")]
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
238 Message::BinaryPrompt(p) => Ok((
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
239 Style::BinaryPrompt,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
240 CBinaryData::alloc(p.question())?.cast(),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
241 )),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
242 #[cfg(not(feature = "linux-pam-extensions"))]
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
243 Message::RadioPrompt(_) | Message::BinaryPrompt(_) => Err(ErrorCode::ConversationError),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
244 }?;
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
245 Ok(Self {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
246 style: style.into(),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
247 data,
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
248 _marker: Default::default(),
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
249 })
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
250 }
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 impl Drop for Question {
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
254 fn drop(&mut self) {
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
255 // 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
256 // 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
257 unsafe {
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
258 // 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
259 // 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
260 if let Ok(style) = Style::try_from(self.style) {
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
261 match style {
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
262 #[cfg(feature = "linux-pam-extensions")]
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
263 Style::BinaryPrompt => {
73
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
264 if let Some(d) = self.data.cast::<CBinaryData>().as_mut() {
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
265 d.zero_contents()
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
266 }
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
267 }
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
268 #[cfg(feature = "linux-pam-extensions")]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
269 Style::RadioType => memory::zero_c_string(self.data.cast()),
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
270 Style::TextInfo
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
271 | Style::ErrorMsg
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
272 | Style::PromptEchoOff
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
273 | Style::PromptEchoOn => memory::zero_c_string(self.data.cast()),
71
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
274 }
58f9d2a4df38 Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents: 70
diff changeset
275 };
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
276 memory::free(self.data);
73
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
277 self.data = ptr::null_mut();
69
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
278 }
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
279 }
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
280 }
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
281
77
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
282 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
283 type Error = ErrorCode;
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
284 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
285 let style: Style = question
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
286 .style
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
287 .try_into()
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
288 .map_err(|_| ErrorCode::ConversationError)?;
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
289 // 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
290 // 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
291 let prompt = unsafe {
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
292 match style {
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
293 Style::PromptEchoOff => {
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
294 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
295 }
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
296 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
297 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
298 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
299 #[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
300 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
301 #[cfg(feature = "linux-pam-extensions")]
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
302 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
303 }
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
304 };
351bdc13005e Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents: 75
diff changeset
305 Ok(prompt)
73
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
306 }
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
307 }
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
308
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
309 #[cfg(test)]
ac6881304c78 Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents: 72
diff changeset
310 mod tests {
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
311
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
312 macro_rules! assert_matches {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
313 ($id:ident => $variant:path, $q:expr) => {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
314 if let $variant($id) = $id {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
315 assert_eq!($q, $id.question());
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
316 } else {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
317 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
318 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
319 };
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
320 }
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
321
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
322 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
323 mod $fn_name {
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
324 use super::super::*;
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
325 #[test]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
326 fn standard() {
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
327 let interrogation = <$typ>::new(&[
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
328 MaskedQAndA::new("hocus pocus").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
329 QAndA::new("what").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
330 QAndA::new("who").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
331 InfoMsg::new("hey").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
332 ErrorMsg::new("gasp").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
333 ])
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
334 .unwrap();
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
335 let indirect = interrogation.ptr();
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
336
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
337 let remade = unsafe { $typ::borrow_ptr(indirect, interrogation.len()) };
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
338 let messages: Vec<OwnedMessage> = remade
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
339 .map(TryInto::try_into)
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
340 .collect::<Result<_>>()
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
341 .unwrap();
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
342 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
343 assert_matches!(masked => OwnedMessage::MaskedPrompt, "hocus pocus");
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
344 assert_matches!(what => OwnedMessage::Prompt, "what");
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
345 assert_matches!(who => OwnedMessage::Prompt, "who");
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
346 assert_matches!(hey => OwnedMessage::Info, "hey");
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
347 assert_matches!(gasp => OwnedMessage::Error, "gasp");
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
348 }
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
349
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
350 #[test]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
351 #[cfg(not(feature = "linux-pam-extensions"))]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
352 fn no_linux_extensions() {
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
353 use crate::conv::{BinaryQAndA, RadioQAndA};
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
354 <$typ>::new(&[
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
355 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
356 RadioQAndA::new("you must choose").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
357 ]).unwrap_err();
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
358 }
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
359
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
360 #[test]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
361 #[cfg(feature = "linux-pam-extensions")]
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
362 fn linux_extensions() {
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
363 let interrogation = <$typ>::new(&[
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
364 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
365 RadioQAndA::new("you must choose").message(),
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
366 ]).unwrap();
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
367 let indirect = interrogation.ptr();
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
368
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
369 let remade = unsafe { $typ::borrow_ptr(indirect, interrogation.len()) };
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
370 let messages: Vec<OwnedMessage> = unsafe { remade }
87
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
371 .map(TryInto::try_into)
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
372 .collect::<Result<_>>()
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
373 .unwrap();
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
374 let [bin, choose] = messages.try_into().unwrap();
05291b601f0a Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents: 85
diff changeset
375 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
376 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
377 }
78
002adfb98c5c Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents: 77
diff changeset
378 }
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
379 }}
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents: 79
diff changeset
380
89
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
381 tests!(test_xsso<XSsoQuestions>);
dd3e9c4bcde3 Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents: 87
diff changeset
382 tests!(test_linux<LinuxPamQuestions>);
69
8f3ae0c7ab92 Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents: 66
diff changeset
383 }