aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api.rs11
-rw-r--r--src/bin/rbw-agent/actions.rs31
-rw-r--r--src/bin/rbw/commands.rs32
-rw-r--r--src/edit.rs10
-rw-r--r--src/error.rs5
-rw-r--r--src/pinentry.rs8
6 files changed, 71 insertions, 26 deletions
diff --git a/src/api.rs b/src/api.rs
index bee0606..e770013 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -831,6 +831,10 @@ impl Client {
"auth-email",
crate::base64::encode_url_safe_no_pad(email),
)
+ .header(
+ "user-agent",
+ format!("rbw/{}", env!("CARGO_PKG_VERSION")),
+ )
.send()
.await
.map_err(|source| Error::Reqwest { source })?;
@@ -1041,7 +1045,12 @@ impl Client {
history: &[crate::db::HistoryEntry],
) -> Result<()> {
let mut req = CiphersPutReq {
- ty: 1,
+ ty: match data {
+ crate::db::EntryData::Login {..} => 1,
+ crate::db::EntryData::SecureNote {..} => 2,
+ crate::db::EntryData::Card {..} => 3,
+ crate::db::EntryData::Identity {..} => 4,
+ },
folder_id: folder_uuid.map(std::string::ToString::to_string),
organization_id: org_id.map(std::string::ToString::to_string),
name: name.to_string(),
diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs
index 58432b1..ff39510 100644
--- a/src/bin/rbw-agent/actions.rs
+++ b/src/bin/rbw-agent/actions.rs
@@ -10,8 +10,7 @@ pub async fn register(
let url_str = config_base_url().await?;
let url = reqwest::Url::parse(&url_str)
.context("failed to parse base url")?;
- let Some(host) = url.host_str()
- else {
+ let Some(host) = url.host_str() else {
return Err(anyhow::anyhow!(
"couldn't find host in rbw base url {}",
url_str
@@ -88,8 +87,7 @@ pub async fn login(
let url_str = config_base_url().await?;
let url = reqwest::Url::parse(&url_str)
.context("failed to parse base url")?;
- let Some(host) = url.host_str()
- else {
+ let Some(host) = url.host_str() else {
return Err(anyhow::anyhow!(
"couldn't find host in rbw base url {}",
url_str
@@ -330,8 +328,7 @@ async fn login_success(
sync(None, state.clone()).await?;
let db = load_db().await?;
- let Some(protected_private_key) = db.protected_private_key
- else {
+ let Some(protected_private_key) = db.protected_private_key else {
return Err(anyhow::anyhow!(
"failed to find protected private key in db"
));
@@ -369,15 +366,11 @@ pub async fn unlock(
if state.lock().await.needs_unlock() {
let db = load_db().await?;
- let Some(kdf) = db.kdf
- else {
- return Err(anyhow::anyhow!(
- "failed to find kdf type in db"
- ));
+ let Some(kdf) = db.kdf else {
+ return Err(anyhow::anyhow!("failed to find kdf type in db"));
};
- let Some(iterations) = db.iterations
- else {
+ let Some(iterations) = db.iterations else {
return Err(anyhow::anyhow!(
"failed to find number of iterations in db"
));
@@ -386,14 +379,12 @@ pub async fn unlock(
let memory = db.memory;
let parallelism = db.parallelism;
- let Some(protected_key) = db.protected_key
- else {
+ let Some(protected_key) = db.protected_key else {
return Err(anyhow::anyhow!(
"failed to find protected key in db"
));
};
- let Some(protected_private_key) = db.protected_private_key
- else {
+ let Some(protected_private_key) = db.protected_private_key else {
return Err(anyhow::anyhow!(
"failed to find protected private key in db"
));
@@ -543,8 +534,7 @@ pub async fn decrypt(
org_id: Option<&str>,
) -> anyhow::Result<()> {
let state = state.lock().await;
- let Some(keys) = state.key(org_id)
- else {
+ let Some(keys) = state.key(org_id) else {
return Err(anyhow::anyhow!(
"failed to find decryption keys in in-memory state"
));
@@ -570,8 +560,7 @@ pub async fn encrypt(
org_id: Option<&str>,
) -> anyhow::Result<()> {
let state = state.lock().await;
- let Some(keys) = state.key(org_id)
- else {
+ let Some(keys) = state.key(org_id) else {
return Err(anyhow::anyhow!(
"failed to find encryption keys in in-memory state"
));
diff --git a/src/bin/rbw/commands.rs b/src/bin/rbw/commands.rs
index 4ea3529..aa7f5b9 100644
--- a/src/bin/rbw/commands.rs
+++ b/src/bin/rbw/commands.rs
@@ -671,12 +671,17 @@ impl std::convert::TryFrom<&String> for ListField {
}
}
-const HELP: &str = r#"
+const HELP_PW: &str = r#"
# The first line of this file will be the password, and the remainder of the
# file (after any blank lines after the password) will be stored as a note.
# Lines with leading # will be ignored.
"#;
+const HELP_NOTES: &str = r#"
+# The content of this file will be stored as a note.
+# Lines with leading # will be ignored.
+"#;
+
pub fn config_show() -> anyhow::Result<()> {
let config = rbw::config::Config::load()?;
serde_json::to_writer_pretty(std::io::stdout(), &config)
@@ -939,7 +944,7 @@ pub fn add(
.map(|username| crate::actions::encrypt(username, None))
.transpose()?;
- let contents = rbw::edit::edit("", HELP)?;
+ let contents = rbw::edit::edit("", HELP_PW)?;
let (password, notes) = parse_editor(&contents);
let password = password
@@ -1140,7 +1145,7 @@ pub fn edit(
contents.push_str(&format!("\n{notes}\n"));
}
- let contents = rbw::edit::edit(&contents, HELP)?;
+ let contents = rbw::edit::edit(&contents, HELP_NOTES)?;
let (password, notes) = parse_editor(&contents);
let password = password
@@ -1188,6 +1193,27 @@ pub fn edit(
};
(data, notes, history)
}
+ DecryptedData::SecureNote {} =>
+ {
+ let data = rbw::db::EntryData::SecureNote {};
+
+ let editor_content = match decrypted.notes {
+ Some(notes) => format!("{notes}\n"),
+ None => format!("\n"),
+ };
+ let contents = rbw::edit::edit(&editor_content, HELP_NOTES)?;
+
+ // prepend blank line to be parsed as pw by `parse_editor`
+ let (_, notes) = parse_editor(&format!("\n{contents}\n"));
+
+ let notes = notes
+ .map(|notes| {
+ crate::actions::encrypt(&notes, entry.org_id.as_deref())
+ })
+ .transpose()?;
+
+ (data, notes, entry.history)
+ }
_ => {
return Err(anyhow::anyhow!(
"modifications are only supported for login entries"
diff --git a/src/edit.rs b/src/edit.rs
index aa8c7b1..cb59366 100644
--- a/src/edit.rs
+++ b/src/edit.rs
@@ -3,6 +3,14 @@ use crate::prelude::*;
use std::io::{Read as _, Write as _};
pub fn edit(contents: &str, help: &str) -> Result<String> {
+ if ! atty::is(atty::Stream::Stdin) {
+ // directly read from piped content
+ return match std::io::read_to_string(std::io::stdin()) {
+ Err(e) => Err(Error::FailedToReadFromStdin{ err: e }),
+ Ok(res) => Ok(res),
+ };
+ }
+
let mut var = "VISUAL";
let editor = std::env::var_os(var).unwrap_or_else(|| {
var = "EDITOR";
@@ -30,7 +38,7 @@ pub fn edit(contents: &str, help: &str) -> Result<String> {
let editor = std::path::Path::new(&editor);
let mut editor_args = vec![];
- #[allow(clippy::single_match)] // more to come
+ #[allow(clippy::single_match_else)] // more to come
match editor.file_name() {
Some(editor) => match editor.to_str() {
Some("vim" | "nvim") => {
diff --git a/src/error.rs b/src/error.rs
index 31edaf3..1ffbcdd 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -21,6 +21,11 @@ pub enum Error {
#[error("failed to parse pinentry output ({out:?})")]
FailedToParsePinentry { out: String },
+ #[error("failed to read from stdin: {err}")]
+ FailedToReadFromStdin {
+ err: std::io::Error,
+ },
+
#[error(
"failed to run editor {}: {err}",
.editor.to_string_lossy(),
diff --git a/src/pinentry.rs b/src/pinentry.rs
index f7d36c7..e2a83ed 100644
--- a/src/pinentry.rs
+++ b/src/pinentry.rs
@@ -137,6 +137,14 @@ where
.read(&mut data[len..])
.await
.map_err(|source| Error::PinentryReadOutput { source })?;
+ if bytes == 0 {
+ return Err(Error::PinentryReadOutput {
+ source: std::io::Error::new(
+ std::io::ErrorKind::UnexpectedEof,
+ "unexpected EOF",
+ ),
+ });
+ }
len += bytes;
}
}