From b83c9f8df03afec62af2ab353ec21028eff64a7d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 27 Dec 2024 00:00:46 -0500 Subject: [PATCH] don't get stuck when talking to old versions of rbw-agent --- src/bin/rbw-agent/agent.rs | 6 +++--- src/bin/rbw/actions.rs | 37 +++++++++++++++++-------------------- src/protocol.rs | 20 +++++++++++++++++++- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/bin/rbw-agent/agent.rs b/src/bin/rbw-agent/agent.rs index 7330eb8..7ddce57 100644 --- a/src/bin/rbw-agent/agent.rs +++ b/src/bin/rbw-agent/agent.rs @@ -175,16 +175,16 @@ async fn handle_request( }; let set_timeout = match &req.action { rbw::protocol::Action::Register => { - crate::actions::register(sock, &req.environment).await?; + crate::actions::register(sock, &req.environment()).await?; true } rbw::protocol::Action::Login => { - crate::actions::login(sock, state.clone(), &req.environment) + crate::actions::login(sock, state.clone(), &req.environment()) .await?; true } rbw::protocol::Action::Unlock => { - crate::actions::unlock(sock, state.clone(), &req.environment) + crate::actions::unlock(sock, state.clone(), &req.environment()) .await?; true } diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs index 7992924..96ed64f 100644 --- a/src/bin/rbw/actions.rs +++ b/src/bin/rbw/actions.rs @@ -37,10 +37,10 @@ pub fn quit() -> anyhow::Result<()> { else { anyhow::bail!("failed to read pid from pidfile"); }; - sock.send(&rbw::protocol::Request { - environment: get_environment(), - action: rbw::protocol::Action::Quit, - })?; + sock.send(&rbw::protocol::Request::new( + get_environment(), + rbw::protocol::Action::Quit, + ))?; wait_for_exit(pid); Ok(()) } @@ -60,14 +60,14 @@ pub fn decrypt( org_id: Option<&str>, ) -> anyhow::Result { let mut sock = connect()?; - sock.send(&rbw::protocol::Request { - environment: get_environment(), - action: rbw::protocol::Action::Decrypt { + sock.send(&rbw::protocol::Request::new( + get_environment(), + rbw::protocol::Action::Decrypt { cipherstring: cipherstring.to_string(), entry_key: entry_key.map(std::string::ToString::to_string), org_id: org_id.map(std::string::ToString::to_string), }, - })?; + ))?; let res = sock.recv()?; match res { @@ -84,13 +84,13 @@ pub fn encrypt( org_id: Option<&str>, ) -> anyhow::Result { let mut sock = connect()?; - sock.send(&rbw::protocol::Request { - environment: get_environment(), - action: rbw::protocol::Action::Encrypt { + sock.send(&rbw::protocol::Request::new( + get_environment(), + rbw::protocol::Action::Encrypt { plaintext: plaintext.to_string(), org_id: org_id.map(std::string::ToString::to_string), }, - })?; + ))?; let res = sock.recv()?; match res { @@ -110,10 +110,10 @@ pub fn clipboard_store(text: &str) -> anyhow::Result<()> { pub fn version() -> anyhow::Result { let mut sock = connect()?; - sock.send(&rbw::protocol::Request { - environment: get_environment(), - action: rbw::protocol::Action::Version, - })?; + sock.send(&rbw::protocol::Request::new( + get_environment(), + rbw::protocol::Action::Version, + ))?; let res = sock.recv()?; match res { @@ -128,10 +128,7 @@ pub fn version() -> anyhow::Result { fn simple_action(action: rbw::protocol::Action) -> anyhow::Result<()> { let mut sock = connect()?; - sock.send(&rbw::protocol::Request { - environment: get_environment(), - action, - })?; + sock.send(&rbw::protocol::Request::new(get_environment(), action))?; let res = sock.recv()?; match res { diff --git a/src/protocol.rs b/src/protocol.rs index 083273d..19fe5b2 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -15,10 +15,28 @@ pub fn version() -> u32 { #[derive(serde::Serialize, serde::Deserialize, Debug)] pub struct Request { - pub environment: Environment, + tty: Option, + environment: Option, pub action: Action, } +impl Request { + pub fn new(environment: Environment, action: Action) -> Self { + Self { + tty: None, + environment: Some(environment), + action, + } + } + + pub fn environment(self) -> Environment { + self.environment.unwrap_or_else(|| Environment { + tty: self.tty.map(|tty| SerializableOsString(tty.into())), + env_vars: vec![], + }) + } +} + // Taken from https://github.com/gpg/gnupg/blob/36dbca3e6944d13e75e96eace634e58a7d7e201d/common/session-env.c#L62-L91 pub const ENVIRONMENT_VARIABLES: &[&str] = &[ // Used to set ttytype