From baa289ce62f92f6012e36466fd1fec95e238a47e Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 5 Nov 2023 16:21:16 -0600 Subject: [PATCH] keyfork-pinentry: use multiline prompts at least for passphrase --- keyfork-pinentry/Cargo.toml | 3 ++- keyfork-pinentry/src/lib.rs | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/keyfork-pinentry/Cargo.toml b/keyfork-pinentry/Cargo.toml index a3d9170..0beb7fe 100644 --- a/keyfork-pinentry/Cargo.toml +++ b/keyfork-pinentry/Cargo.toml @@ -11,7 +11,8 @@ authors = ["Ryan Heywood "] # categories = ["api-bindings", "command-line-interface"] # license = "MIT OR Apache-2.0" license = "MIT" -edition = "2018" +# edition = "2018" +edition = "2021" [dependencies] nom = { version = "7", default-features = false } diff --git a/keyfork-pinentry/src/lib.rs b/keyfork-pinentry/src/lib.rs index 44d1713..ce930ee 100644 --- a/keyfork-pinentry/src/lib.rs +++ b/keyfork-pinentry/src/lib.rs @@ -69,6 +69,24 @@ pub fn default_binary() -> Result { which::which("pinentry-curses").map_err(Into::into) } +fn convert_multiline(line: &str) -> String { + // convert into multiline + let mut converted_line = String::new(); + let mut last_end = 0; + for (start, part) in line.match_indices(&['\n', '\r', '%']) { + converted_line.push_str(line.get(last_end..start).unwrap()); + converted_line.push_str(match part { + "\n" => "%0A", + "\r" => "%0D", + "%" => "%25", + fb => panic!("expected index given to match_indices, got: {fb}"), + }); + last_end = start + part.len(); + } + converted_line.push_str(line.get(last_end..line.len()).unwrap()); + converted_line +} + /// A dialog for requesting a passphrase from the user. pub struct PassphraseInput<'a> { binary: PathBuf, @@ -211,7 +229,7 @@ impl<'a> PassphraseInput<'a> { pinentry.send_request("SETTITLE", Some(title))?; } if let Some(desc) = &self.description { - pinentry.send_request("SETDESC", Some(desc))?; + pinentry.send_request("SETDESC", Some(convert_multiline(desc).as_ref()))?; } if let Some(error) = &self.error { pinentry.send_request("SETERROR", Some(error))?;