Mercurial > crates > nonstick
annotate src/libpam/question.rs @ 183:4f46681b3f54 default tip
Catch a few stray cargo fmt things.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 30 Jul 2025 18:43:07 -0400 |
parents | a1bb1d013567 |
children |
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 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
3 use crate::conv::{ErrorMsg, Exchange, InfoMsg, MaskedQAndA, QAndA}; |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
4 use crate::libpam::conversation::OwnedExchange; |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
5 use crate::libpam::memory; |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
6 use crate::ErrorCode; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
7 use crate::Result; |
152
4d11b2e7da83
fix dumb `use x as x` in question.rs.
Paul Fisher <paul@pfish.zone>
parents:
148
diff
changeset
|
8 use libpam_sys_helpers; |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
9 use std::ffi::{c_int, c_void, CStr, OsStr}; |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
10 use std::os::unix::ffi::OsStrExt; |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
11 use std::ptr::NonNull; |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
12 |
180
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
13 memory::num_enum! { |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
14 /// The C enum values for messages shown to the user. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
15 enum Style(i32) { |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
16 /// Requests information from the user; will be masked when typing. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
17 PromptEchoOff = libpam_sys::PAM_PROMPT_ECHO_OFF, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
18 /// Requests information from the user; will not be masked. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
19 PromptEchoOn = libpam_sys::PAM_PROMPT_ECHO_ON, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
20 /// An error message. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
21 ErrorMsg = libpam_sys::PAM_ERROR_MSG, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
22 /// An informational message. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
23 TextInfo = libpam_sys::PAM_TEXT_INFO, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
24 /// Yes/No/Maybe conditionals. A Linux-PAM extension. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
25 #[cfg(feature = "linux-pam-ext")] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
26 RadioType = libpam_sys::PAM_RADIO_TYPE, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
27 /// For server–client non-human interaction. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
28 /// |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
29 /// NOT part of the X/Open PAM specification. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
30 /// A Linux-PAM extension. |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
31 #[cfg(feature = "linux-pam-ext")] |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
32 BinaryPrompt = libpam_sys::PAM_BINARY_PROMPT, |
a1bb1d013567
Remove `syn` from the dependency tree by implementing our own num_enum.
Paul Fisher <paul@pfish.zone>
parents:
179
diff
changeset
|
33 } |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
34 } |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
35 |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
36 /// A question sent by PAM or a module to an application. |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
37 /// |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
38 /// PAM refers to this as a "message", but we call it a question |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
39 /// to avoid confusion. |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
40 /// |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
41 /// This question, and its internal data, is owned by its creator |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
42 /// (either the module or PAM itself). |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
43 #[repr(C)] |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
44 #[derive(Debug)] |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
45 pub struct Question { |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
46 /// The style of message to request. |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
47 pub style: c_int, |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
48 /// A description of the data requested. |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
49 /// |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
50 /// For most requests, this will be an owned [`CStr`], |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
51 /// but for requests with style `PAM_BINARY_PROMPT`, |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
52 /// this will be `CBinaryData` (a Linux-PAM extension). |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
53 pub data: Option<NonNull<c_void>>, |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
54 } |
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
55 |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
56 impl Question { |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
57 /// 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
|
58 /// |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
59 /// # Safety |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
60 /// |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
61 /// It's up to you to pass this only on types with a string value. |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
62 unsafe fn string_data(&self) -> &OsStr { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
63 match self.data.as_ref() { |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
64 None => "".as_ref(), |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
65 Some(data) => OsStr::from_bytes(CStr::from_ptr(data.as_ptr().cast()).to_bytes()), |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
66 } |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
67 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
68 |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
69 /// 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
|
70 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
|
71 self.data |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
72 .as_ref() |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
143
diff
changeset
|
73 .map(|data| libpam_sys_helpers::BinaryPayload::contents(data.as_ptr().cast())) |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
74 .unwrap_or_default() |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
75 } |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
76 } |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
77 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
78 impl TryFrom<&Exchange<'_>> for Question { |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
79 type Error = ErrorCode; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
80 fn try_from(msg: &Exchange) -> Result<Self> { |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
81 let alloc = |style, text: &OsStr| -> Result<_> { |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
82 Ok((style, unsafe { |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
83 memory::CHeapBox::cast(memory::CHeapString::new(text.as_bytes()).into_box()) |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
84 })) |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
85 }; |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
86 // We will only allocate heap data if we have a valid input. |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
87 let (style, data): (_, memory::CHeapBox<c_void>) = match *msg { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
88 Exchange::MaskedPrompt(p) => alloc(Style::PromptEchoOff, p.question()), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
89 Exchange::Prompt(p) => alloc(Style::PromptEchoOn, p.question()), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
90 Exchange::Error(p) => alloc(Style::ErrorMsg, p.question()), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
91 Exchange::Info(p) => alloc(Style::TextInfo, p.question()), |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
92 #[cfg(feature = "linux-pam-ext")] |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
93 Exchange::RadioPrompt(p) => alloc(Style::RadioType, p.question()), |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
94 #[cfg(feature = "linux-pam-ext")] |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
95 Exchange::BinaryPrompt(p) => { |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
96 let (data, typ) = p.question(); |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
97 let payload = memory::CHeapPayload::new(data, typ)?.into_inner(); |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
98 Ok((Style::BinaryPrompt, unsafe { |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
99 memory::CHeapBox::cast(payload) |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
100 })) |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
101 } |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
102 #[cfg(not(feature = "linux-pam-ext"))] |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
103 Exchange::RadioPrompt(_) | Exchange::BinaryPrompt(_) => { |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
104 Err(ErrorCode::ConversationError) |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
105 } |
89
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 Ok(Self { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
108 style: style.into(), |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
109 data: Some(memory::CHeapBox::into_ptr(data)), |
89
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
110 }) |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
111 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
112 } |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
113 |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
114 impl Drop for Question { |
dd3e9c4bcde3
Simplify memory management in Questions.
Paul Fisher <paul@pfish.zone>
parents:
87
diff
changeset
|
115 fn drop(&mut self) { |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
116 // 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
|
117 // 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
|
118 unsafe { |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
119 // 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
|
120 // 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
|
121 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
|
122 let _ = match style { |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
123 #[cfg(feature = "linux-pam-ext")] |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
124 Style::BinaryPrompt => self |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
125 .data |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
126 .as_mut() |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
143
diff
changeset
|
127 .map(|p| libpam_sys_helpers::BinaryPayload::zero(p.as_ptr().cast())), |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
128 #[cfg(feature = "linux-pam-ext")] |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
129 Style::RadioType => self |
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
130 .data |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
131 .as_mut() |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
132 .map(|p| memory::CHeapString::zero(p.cast())), |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
133 Style::TextInfo |
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
134 | Style::ErrorMsg |
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
135 | Style::PromptEchoOff |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
136 | Style::PromptEchoOn => self |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
137 .data |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
138 .as_mut() |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
139 .map(|p| memory::CHeapString::zero(p.cast())), |
98
b87100c5eed4
Start on environment variables, and make pointers nicer.
Paul Fisher <paul@pfish.zone>
parents:
93
diff
changeset
|
140 }; |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
141 }; |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
142 let _ = self.data.map(|p| memory::CHeapBox::from_ptr(p)); |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
143 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
144 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
145 } |
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
146 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
147 impl<'a> TryFrom<&'a Question> for OwnedExchange<'a> { |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
148 type Error = ErrorCode; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
149 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
|
150 let style: Style = question |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
151 .style |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
152 .try_into() |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
153 .map_err(|_| ErrorCode::ConversationError)?; |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
154 // 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
|
155 // 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
|
156 let prompt = unsafe { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
157 match style { |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
158 Style::PromptEchoOff => { |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
159 Self::MaskedPrompt(MaskedQAndA::new(question.string_data())) |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
160 } |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
161 Style::PromptEchoOn => Self::Prompt(QAndA::new(question.string_data())), |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
162 Style::ErrorMsg => Self::Error(ErrorMsg::new(question.string_data())), |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
163 Style::TextInfo => Self::Info(InfoMsg::new(question.string_data())), |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
164 #[cfg(feature = "linux-pam-ext")] |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
165 Style::RadioType => { |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
166 Self::RadioPrompt(crate::conv::RadioQAndA::new(question.string_data())) |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
167 } |
108
e97534be35e3
Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
106
diff
changeset
|
168 #[cfg(feature = "linux-pam-ext")] |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
169 Style::BinaryPrompt => { |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
170 Self::BinaryPrompt(crate::conv::BinaryQAndA::new(question.binary_data())) |
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
171 } |
77
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
172 } |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
173 }; |
351bdc13005e
Update the libpam module to work with the new structure.
Paul Fisher <paul@pfish.zone>
parents:
75
diff
changeset
|
174 Ok(prompt) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
175 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
176 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
177 |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
178 #[cfg(feature = "linux-pam-ext")] |
148
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
143
diff
changeset
|
179 impl From<libpam_sys_helpers::TooBigError> for ErrorCode { |
4b3a5095f68c
Move libpam-sys helpers into their own library.
Paul Fisher <paul@pfish.zone>
parents:
143
diff
changeset
|
180 fn from(_: libpam_sys_helpers::TooBigError) -> Self { |
139
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
181 ErrorCode::BufferError |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
182 } |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
183 } |
33b9622ed6d2
Remove redundant memory management in nonstick::libpam; fix UB.
Paul Fisher <paul@pfish.zone>
parents:
130
diff
changeset
|
184 |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
185 #[cfg(test)] |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
186 mod tests { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
187 use super::*; |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
188 |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
189 macro_rules! assert_matches { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
190 (($variant:path, $q:expr), $input:expr) => { |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
191 let input = $input; |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
192 let exc = input.exchange(); |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
193 if let $variant(msg) = exc { |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
194 assert_eq!($q, msg.question()); |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
195 } else { |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
196 panic!( |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
197 "want enum variant {v}, got {exc:?}", |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
198 v = stringify!($variant) |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
199 ); |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
200 } |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
201 }; |
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
202 } |
78
002adfb98c5c
Rename files, reorder structs, remove annoying BorrowedBinaryData type.
Paul Fisher <paul@pfish.zone>
parents:
77
diff
changeset
|
203 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
204 // TODO: Test TryFrom<Exchange> for Question/OwnedQuestion. |
87
05291b601f0a
Well and truly separate the Linux extensions.
Paul Fisher <paul@pfish.zone>
parents:
85
diff
changeset
|
205 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
206 #[test] |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
207 fn standard() { |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
208 assert_matches!( |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
209 (Exchange::MaskedPrompt, "hocus pocus"), |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
210 MaskedQAndA::new("hocus pocus".as_ref()) |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
211 ); |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
212 assert_matches!((Exchange::Prompt, "what"), QAndA::new("what".as_ref())); |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
213 assert_matches!((Exchange::Prompt, "who"), QAndA::new("who".as_ref())); |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
214 assert_matches!((Exchange::Info, "hey"), InfoMsg::new("hey".as_ref())); |
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
215 assert_matches!((Exchange::Error, "gasp"), ErrorMsg::new("gasp".as_ref())); |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
216 } |
80
5aa1a010f1e8
Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
79
diff
changeset
|
217 |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
218 #[test] |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
219 #[cfg(feature = "linux-pam-ext")] |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
220 fn linux_extensions() { |
141
a508a69c068a
Remove a lot of Results from functions.
Paul Fisher <paul@pfish.zone>
parents:
139
diff
changeset
|
221 use crate::conv::{BinaryQAndA, RadioQAndA}; |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
222 assert_matches!( |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
223 (Exchange::BinaryPrompt, (&[5, 4, 3, 2, 1][..], 66)), |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
224 BinaryQAndA::new((&[5, 4, 3, 2, 1], 66)) |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
225 ); |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
226 assert_matches!( |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
227 (Exchange::RadioPrompt, "you must choose"), |
143
ebb71a412b58
Turn everything into OsString and Just Walk Out! for strings with nul.
Paul Fisher <paul@pfish.zone>
parents:
141
diff
changeset
|
228 RadioQAndA::new("you must choose".as_ref()) |
130
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
229 ); |
80c07e5ab22f
Transfer over (almost) completely to using libpam-sys.
Paul Fisher <paul@pfish.zone>
parents:
113
diff
changeset
|
230 } |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
66
diff
changeset
|
231 } |