Skip to content

Commit

Permalink
mount: replace rpassword with rustix::termios
Browse files Browse the repository at this point in the history
because rpassword unconditionally open()s /dev/tty, it fails with ENXIO
on the console without workarounds like busybox's cttyhack. in contrast,
bcachefs unlock works fine on console, so change the passphrase prompt
logic in mount to be closer to what it is in unlock.

Signed-off-by: Lauri Tirkkonen <[email protected]>
Signed-off-by: Kent Overstreet <[email protected]>
  • Loading branch information
lotheac authored and Kent Overstreet committed Jul 15, 2024
1 parent 9e12dd0 commit 7ebd67e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
27 changes: 3 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ udev = "0.7.0"
uuid = "1.2.2"
errno = "0.2"
either = "1.5"
rpassword = "7"
bch_bindgen = { path = "bch_bindgen" }
byteorder = "1.3"
strum = { version = "0.26", features = ["derive"] }
strum_macros = "0.26"
zeroize = { version = "1", features = ["std", "zeroize_derive"] }
rustix = { version = "0.38.34", features = ["termios"] }

[dependencies.env_logger]
version = "0.10"
Expand Down
16 changes: 14 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use bch_bindgen::{
};
use byteorder::{LittleEndian, ReadBytesExt};
use log::info;
use rustix::termios;
use uuid::Uuid;
use zeroize::{ZeroizeOnDrop, Zeroizing};

Expand Down Expand Up @@ -151,9 +152,20 @@ impl Passphrase {

// blocks indefinitely if no input is available on stdin
pub fn new_from_prompt() -> Result<Self> {
let passphrase = Zeroizing::new(rpassword::prompt_password("Enter passphrase: ")?);
let old = termios::tcgetattr(stdin())?;
let mut new = old.clone();
new.local_modes.remove(termios::LocalModes::ECHO);
termios::tcsetattr(stdin(), termios::OptionalActions::Flush, &new)?;

Ok(Self(CString::new(passphrase.trim_end_matches('\n'))?))
eprint!("Enter passphrase: ");

let mut line = Zeroizing::new(String::new());
let res = stdin().read_line(&mut line);
termios::tcsetattr(stdin(), termios::OptionalActions::Flush, &old)?;
eprintln!("");
res?;

Ok(Self(CString::new(line.trim_end_matches('\n'))?))
}

// blocks indefinitely if no input is available on stdin
Expand Down

0 comments on commit 7ebd67e

Please sign in to comment.