Skip to content

Commit

Permalink
don't get stuck when talking to old versions of rbw-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
doy committed Dec 27, 2024
1 parent cf2f04e commit b83c9f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/bin/rbw-agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
37 changes: 17 additions & 20 deletions src/bin/rbw/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand All @@ -60,14 +60,14 @@ pub fn decrypt(
org_id: Option<&str>,
) -> anyhow::Result<String> {
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 {
Expand All @@ -84,13 +84,13 @@ pub fn encrypt(
org_id: Option<&str>,
) -> anyhow::Result<String> {
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 {
Expand All @@ -110,10 +110,10 @@ pub fn clipboard_store(text: &str) -> anyhow::Result<()> {

pub fn version() -> anyhow::Result<u32> {
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 {
Expand All @@ -128,10 +128,7 @@ pub fn version() -> anyhow::Result<u32> {
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 {
Expand Down
20 changes: 19 additions & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,28 @@ pub fn version() -> u32 {

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct Request {
pub environment: Environment,
tty: Option<String>,
environment: Option<Environment>,
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
Expand Down

0 comments on commit b83c9f8

Please sign in to comment.