summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-01-17 21:15:14 +0100
committerqkzk <qu3nt1n@gmail.com>2023-01-17 21:15:14 +0100
commitbd866af20ce0c772a62b75b30a3330202201ece0 (patch)
treef2b05437963b8c9b53d99ae39db00cd0759f3112
parent3a89a4ce063b8aca2338d04356ccc10e890680bc (diff)
password input, mount, umount
-rw-r--r--src/cryptsetup.rs48
-rw-r--r--src/event_exec.rs39
-rw-r--r--src/input.rs9
-rw-r--r--src/main.rs6
-rw-r--r--src/mode.rs9
-rw-r--r--src/term_manager.rs8
6 files changed, 79 insertions, 40 deletions
diff --git a/src/cryptsetup.rs b/src/cryptsetup.rs
index 83ce3a6..6381350 100644
--- a/src/cryptsetup.rs
+++ b/src/cryptsetup.rs
@@ -1,6 +1,8 @@
use std::io::Write;
use std::process::{Command, Stdio};
+use log::info;
+
use crate::fm_error::{FmError, FmResult};
use crate::impl_selectable_content;
use crate::utils::current_username;
@@ -11,11 +13,17 @@ pub enum PasswordKind {
CRYPTSETUP,
}
+#[derive(Debug, Clone, Copy)]
+pub enum EncryptedAction {
+ MOUNT,
+ UMOUNT,
+}
+
impl std::fmt::Display for PasswordKind {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let asker = match self {
- Self::SUDO => "sudo",
- Self::CRYPTSETUP => "cryptsetup",
+ Self::SUDO => "sudo ",
+ Self::CRYPTSETUP => "device ",
};
write!(f, "{}", asker)
}
@@ -65,6 +73,7 @@ impl PasswordHolder {
}
pub fn has_sudo(&self) -> bool {
+ info!("has sudo ? {:?}", self);
self.sudo.is_some()
}
@@ -101,7 +110,7 @@ pub fn filter_crypto_devices_lines(output: String, key: &str) -> Vec<String> {
/// run a sudo command requiring a password (generally to establish the password.)
/// Since I can't send 2 passwords at a time, it will only work with the sudo password
fn sudo_password(args: &[String], password: &str) -> FmResult<(String, String)> {
- println!("sudo {:?}", args);
+ info!("sudo {:?}", args);
let mut child = Command::new("sudo")
.args(args)
.stdin(Stdio::piped())
@@ -126,7 +135,7 @@ fn sudo_password(args: &[String], password: &str) -> FmResult<(String, String)>
/// Run a passwordless sudo command.
/// Returns stdout & stderr
fn sudo(args: &[String]) -> FmResult<(String, String)> {
- println!("sudo {:?}", args);
+ info!("sudo {:?}", args);
let child = Command::new("sudo")
.args(args)
.stdin(Stdio::piped())
@@ -221,7 +230,7 @@ impl CryptoDevice {
]
}
- pub fn open_mount(&mut self, username: &str, passwords: &PasswordHolder) -> FmResult<bool> {
+ pub fn open_mount(&mut self, username: &str, passwords: &PasswordHolder) -> FmResult<()> {
if self.is_mounted()? {
Err(FmError::custom(
"luks open mount",
@@ -233,44 +242,40 @@ impl CryptoDevice {
&["-S".to_owned(), "ls".to_owned(), "/root".to_owned()],
&passwords.sudo()?,
)?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// open
let output =
sudo_password(&self.format_luksopen_parameters(), &passwords.cryptsetup()?)?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// mkdir
let output = sudo(&self.format_mkdir_parameters(username))?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// mount
let output = sudo(&self.format_mount_parameters(username))?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// sudo -t
sudo(&["-k".to_owned()])?;
- println!("wait a few seconds...");
- std::thread::sleep(std::time::Duration::from_secs(10));
- self.is_mounted()
+ Ok(())
}
}
- pub fn umount_close(&mut self, username: &str, passwords: &PasswordHolder) -> FmResult<bool> {
+ pub fn umount_close(&mut self, username: &str, passwords: &PasswordHolder) -> FmResult<()> {
// sudo
let output = sudo_password(
&["-S".to_owned(), "ls".to_owned(), "/root".to_owned()],
&passwords.sudo()?,
)?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// unmount
let output = sudo(&self.format_umount_parameters(username))?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// close
let output = sudo(&self.format_luksclose_parameters())?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
// sudo -t
let output = sudo(&["-k".to_owned()])?;
- println!("stdout: {}\nstderr: {}", output.0, output.1);
- println!("wait a few seconds...");
- std::thread::sleep(std::time::Duration::from_secs(5));
- Ok(!self.is_mounted()?)
+ info!("stdout: {}\nstderr: {}", output.0, output.1);
+ Ok(())
}
pub fn is_mounted(&self) -> FmResult<bool> {
@@ -287,8 +292,7 @@ impl CryptoDevice {
let is_mounted = self.is_mounted()?;
let mounted_char = if is_mounted { 'm' } else { 'u' };
let opened_char = if self.is_opened()? { 'o' } else { 'c' };
- let address = "/dev/sdb";
- let mut s = format!("{} {} {}", mounted_char, opened_char, address);
+ let mut s = format!("{} {} {}", mounted_char, opened_char, self.path);
if let Some(mountpoints) = &self.mountpoints {
s.push_str(" -> ");
diff --git a/src/event_exec.rs b/src/event_exec.rs
index 59cb113..f925042 100644
--- a/src/event_exec.rs
+++ b/src/event_exec.rs
@@ -13,6 +13,7 @@ use crate::constant_strings_paths::DEFAULT_DRAGNDROP;
use crate::constant_strings_paths::NVIM_RPC_SENDER;
use crate::content_window::RESERVED_ROWS;
use crate::copy_move::CopyMove;
+use crate::cryptsetup::EncryptedAction;
use crate::cryptsetup::PasswordKind;
use crate::fileinfo::FileKind;
use crate::filter::FilterKind;
@@ -1255,6 +1256,7 @@ impl EventExec {
/// Reset to normal mode afterwards.
pub fn event_enter(status: &mut Status) -> FmResult<()> {
let mut must_refresh = true;
+ let mut must_reset_mode = true;
match status.selected_non_mut().mode {
Mode::InputSimple(InputSimple::Rename) => EventExec::exec_rename(status.selected())?,
Mode::InputSimple(InputSimple::Newfile) => EventExec::exec_newfile(status.selected())?,
@@ -1265,8 +1267,14 @@ impl EventExec {
must_refresh = false;
EventExec::exec_filter(status)?
}
- Mode::InputSimple(InputSimple::Password(password_kind)) => {
- EventExec::exec_store_password(status, password_kind)?
+ Mode::InputSimple(InputSimple::Password(password_kind, encrypted_action)) => {
+ must_refresh = false;
+ must_reset_mode = false;
+ EventExec::exec_store_password(status, password_kind)?;
+ match encrypted_action {
+ EncryptedAction::MOUNT => EventExec::event_mount_encrypted_drive(status)?,
+ EncryptedAction::UMOUNT => EventExec::event_umount_encrypted_drive(status)?,
+ }
}
Mode::Navigate(Navigate::Jump) => EventExec::exec_jump(status)?,
Mode::Navigate(Navigate::History) => EventExec::exec_history(status.selected())?,
@@ -1289,7 +1297,9 @@ impl EventExec {
};
status.selected().input.reset();
- status.selected().reset_mode();
+ if must_reset_mode {
+ status.selected().reset_mode();
+ }
if must_refresh {
Self::refresh_status(status)?;
}
@@ -1342,6 +1352,7 @@ impl EventExec {
/// Refresh the current view, reloading the files. Move the selection to top.
pub fn event_refreshview(status: &mut Status) -> FmResult<()> {
+ status.encrypted_devices.update()?;
Self::refresh_status(status)
}
@@ -1556,31 +1567,38 @@ impl EventExec {
status
.selected()
.set_mode(Mode::Navigate(Navigate::EncryptedDrive));
- status.encrypted_devices.update()
+ Ok(())
}
pub fn event_mount_encrypted_drive(status: &mut Status) -> FmResult<()> {
if !status.encrypted_devices.has_sudo() {
- Self::event_ask_password(status, PasswordKind::SUDO)
+ Self::event_ask_password(status, PasswordKind::SUDO, EncryptedAction::MOUNT)
} else if !status.encrypted_devices.has_cryptsetup() {
- Self::event_ask_password(status, PasswordKind::CRYPTSETUP)
+ Self::event_ask_password(status, PasswordKind::CRYPTSETUP, EncryptedAction::MOUNT)
} else {
status.encrypted_devices.mount_selected()
}
}
pub fn event_umount_encrypted_drive(status: &mut Status) -> FmResult<()> {
- if status.encrypted_devices.has_sudo() {
- Self::event_ask_password(status, PasswordKind::SUDO)
+ if !status.encrypted_devices.has_sudo() {
+ Self::event_ask_password(status, PasswordKind::SUDO, EncryptedAction::UMOUNT)
} else {
status.encrypted_devices.umount_selected()
}
}
- pub fn event_ask_password(status: &mut Status, password_kind: PasswordKind) -> FmResult<()> {
+ pub fn event_ask_password(
+ status: &mut Status,
+ password_kind: PasswordKind,
+ encrypted_action: EncryptedAction,
+ ) -> FmResult<()> {
status
.selected()
- .set_mode(Mode::InputSimple(InputSimple::Password(password_kind)));
+ .set_mode(Mode::InputSimple(InputSimple::Password(
+ password_kind,
+ encrypted_action,
+ )));
Ok(())
}
@@ -1589,6 +1607,7 @@ impl EventExec {
status
.encrypted_devices
.set_password(password_kind, password);
+ info!("encrypted_devices {:?}", status.encrypted_devices);
status.selected().reset_mode();
Ok(())
}
diff --git a/src/input.rs b/src/input.rs
index defa674..bf76cc1 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -65,6 +65,15 @@ impl Input {
self.chars.iter().collect()
}
+ pub fn password(&self) -> String {
+ self.chars
+ .iter()
+ .map(|x| match x {
+ _ => '*',
+ })
+ .collect()
+ }
+
/// Insert an utf-8 char into the input at cursor index.
pub fn insert(&mut self, c: char) {
self.chars.insert(self.cursor_index, c);
diff --git a/src/main.rs b/src/main.rs
index 0402092..e7d9b71 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -69,10 +69,8 @@ fn _main() -> FmResult<()> {
.to_str()
.ok_or_else(|| fm::fm_error::FmError::custom("username", "couldn't read username"))?;
println!("{:?}", crypto_device);
- println!(
- "mounted ? {}",
- crypto_device.open_mount(&username, &password_holder)?
- );
+ crypto_device.open_mount(&username, &password_holder)?;
+
// println!(
// "unmounted ? {}",
// crypto_device.umount_close("quentin", &password_holder)?
diff --git a/src/mode.rs b/src/mode.rs
index e2405f6..e13e6ec 100644
--- a/src/mode.rs
+++ b/src/mode.rs
@@ -1,6 +1,7 @@
use std::fmt;
-use crate::{completion::InputCompleted, cryptsetup::PasswordKind};
+use crate::completion::InputCompleted;
+use crate::cryptsetup::{EncryptedAction, PasswordKind};
/// Different kind of mark actions.
/// Either we jump to an existing mark or we save current path to a mark.
@@ -76,7 +77,7 @@ pub enum InputSimple {
/// Filter by extension, name, directory or no filter
Filter,
///
- Password(PasswordKind),
+ Password(PasswordKind, EncryptedAction),
}
/// Different modes in which we display a bunch of possible destinations.
@@ -132,8 +133,8 @@ impl fmt::Display for Mode {
}
Mode::InputSimple(InputSimple::Marks(_)) => write!(f, "Marks jump:"),
Mode::InputSimple(InputSimple::Filter) => write!(f, "Filter: "),
- Mode::InputSimple(InputSimple::Password(password_kind)) => {
- write!(f, "Password for {}", password_kind)
+ Mode::InputSimple(InputSimple::Password(password_kind, _)) => {
+ write!(f, "{}", password_kind)
}
Mode::InputCompleted(InputCompleted::Exec) => write!(f, "Exec: "),
Mode::InputCompleted(InputCompleted::Goto) => write!(f, "Goto : "),
diff --git a/src/term_manager.rs b/src/term_manager.rs
index b13617d..d00784b 100644
--- a/src/term_manager.rs
+++ b/src/term_manager.rs
@@ -386,6 +386,7 @@ impl<'a> WinSecondary<'a> {
const EDIT_BOX_OFFSET: usize = 9;
const ATTR_YELLOW: Attr = color_to_attr(Color::YELLOW);
const SORT_CURSOR_OFFSET: usize = 37;
+ const PASSWORD_CURSOR_OFFSET: usize = 7;
fn new(status: &'a Status, index: usize) -> Self {
Self {
@@ -409,6 +410,9 @@ impl<'a> WinSecondary<'a> {
Mode::InputSimple(InputSimple::Marks(MarkAction::New)) => {
vec!["Save mark...".to_owned()]
}
+ Mode::InputSimple(InputSimple::Password(password_kind, _encrypted_action)) => {
+ vec![format!("{}", password_kind), tab.input.password()]
+ }
_ => {
vec![
format!("{}", tab.mode.clone()),
@@ -446,6 +450,10 @@ impl<'a> WinSecondary<'a> {
canvas.show_cursor(true)?;
canvas.set_cursor(0, Self::SORT_CURSOR_OFFSET)?;
}
+ Mode::InputSimple(InputSimple::Password(_, _)) => {
+ canvas.show_cursor(true)?;
+ canvas.set_cursor(0, Self::PASSWORD_CURSOR_OFFSET + tab.input.cursor_index)?;
+ }
Mode::InputSimple(_) | Mode::InputCompleted(_) => {
canvas.show_cursor(true)?;
canvas.set_cursor(0, tab.input.cursor_index + Self::EDIT_BOX_OFFSET)?;