From 589adf294f3c1cfd059652c63243f211e4dc4479 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Sun, 22 Nov 2020 20:36:53 +0100 Subject: [PATCH] Ask password from prompt --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/main.rs | 21 ++++++++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 890915c..94fdf3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -352,6 +352,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "rpassword" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d755237fc0f99d98641540e66abac8bc46a0652f19148ac9e21de2da06b326c9" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -428,6 +438,7 @@ version = "0.1.0" dependencies = [ "crossterm 0.18.2", "getopts", + "rpassword", "ssh2", "tempfile", "tui", diff --git a/Cargo.toml b/Cargo.toml index 04ea4d0..905958b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ ssh2 = "0.8.2" tui = { version = "0.12.0", features = ["crossterm"], default-features = false } users = "0.11.0" whoami = "0.9.0" +rpassword = "5.0.0" [dev-dependencies] tempfile = "3" diff --git a/src/main.rs b/src/main.rs index bb57dfe..1c98662 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ const TERMSCP_AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS"); // Crates extern crate getopts; +extern crate rpassword; // External libs use getopts::Options; @@ -41,7 +42,7 @@ mod utils; // namespaces use activity_manager::{ActivityManager, NextActivity}; -use filetransfer::{FileTransfer, sftp_transfer::SftpFileTransfer}; +use filetransfer::{sftp_transfer::SftpFileTransfer, FileTransfer}; use ui::activities::auth_activity::ScpProtocol; /// ### print_usage @@ -132,7 +133,7 @@ fn main() { port = portn; protocol = proto; username = user; - }, + } Err(err) => { eprintln!("Bad address option: {}", err); print_usage(opts); @@ -152,7 +153,7 @@ fn main() { // Get working directory let wrkdir: PathBuf = match env::current_dir() { Ok(dir) => dir, - Err(_) => PathBuf::from("/") + Err(_) => PathBuf::from("/"), }; // Create activity manager let mut manager: ActivityManager = match ActivityManager::new(file_transfer, &wrkdir) { @@ -165,6 +166,20 @@ fn main() { // Initialize client if necessary let mut start_activity: NextActivity = NextActivity::Authentication; if let Some(address) = address { + // Ask password + password = match rpassword::read_password_from_tty(Some("Password: ")) { + Ok(p) => { + if p.len() > 0 { + Some(p) + } else { + None + } + } + Err(_) => { + eprintln!("Could not read password from prompt"); + std::process::exit(255); + } + }; // In this case the first activity will be FileTransfer start_activity = NextActivity::FileTransfer; manager.set_filetransfer_params(address, port, protocol, username, password);