summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-01-16 01:08:04 +0100
committerqkzk <qu3nt1n@gmail.com>2023-01-16 01:08:04 +0100
commit8da1a8aeb886b5980dd5944059b9018219989c55 (patch)
tree9d6f794f1cc44170f5c0b79504070a83d6a97fae /src
parente691e175b50223c1b9add6a7dbe47322f498378d (diff)
still buggy
Diffstat (limited to 'src')
-rw-r--r--src/luks.rs174
-rw-r--r--src/main.rs53
2 files changed, 141 insertions, 86 deletions
diff --git a/src/luks.rs b/src/luks.rs
index 941303b..963e501 100644
--- a/src/luks.rs
+++ b/src/luks.rs
@@ -3,6 +3,36 @@ use std::process::{Command, Stdio};
use crate::fm_error::{FmError, FmResult};
+#[derive(Default)]
+pub struct PasswordHolder {
+ sudo: Option<String>,
+ cryptsetup: Option<String>,
+}
+
+impl PasswordHolder {
+ pub fn set_sudo_password(&mut self, password: &str) {
+ self.sudo = Some(password.to_owned())
+ }
+
+ pub fn set_cryptsetup_password(&mut self, passphrase: &str) {
+ self.cryptsetup = Some(passphrase.to_owned())
+ }
+
+ pub fn cryptsetup(&self) -> FmResult<String> {
+ Ok(self
+ .cryptsetup
+ .clone()
+ .ok_or_else(|| FmError::custom("PasswordHolder", "sudo password isn't set"))?)
+ }
+
+ pub fn sudo(&self) -> FmResult<String> {
+ Ok(self
+ .sudo
+ .clone()
+ .ok_or_else(|| FmError::custom("PasswordHolder", "sudo password isn't set"))?)
+ }
+}
+
/// get devices list from lsblk
/// Return the output of
/// ```bash
@@ -28,6 +58,44 @@ pub fn filter_crypto_devices_lines(output: String) -> Vec<String> {
.collect()
}
+fn run_privileged_command(args: &[String], password: &str) -> FmResult<(String, String)> {
+ println!("sudo, {:?}", args);
+ let mut child = Command::new("sudo")
+ .args(args)
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()?;
+
+ let child_stdin = child
+ .stdin
+ .as_mut()
+ .ok_or_else(|| FmError::custom("run_privileged_command", "couldn't open child stdin"))?;
+ child_stdin.write_all(&format!("{}\n", password).as_bytes())?;
+ drop(child_stdin);
+
+ let output = child.wait_with_output()?;
+ Ok((
+ String::from_utf8(output.stdout)?,
+ String::from_utf8(output.stderr)?,
+ ))
+}
+
+fn run_command(command: &str, args: &[String]) -> FmResult<(String, String)> {
+ println!("{}, {:?}", command, args);
+ let child = Command::new(command)
+ .args(args)
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()?;
+ let output = child.wait_with_output()?;
+ Ok((
+ String::from_utf8(output.stdout)?,
+ String::from_utf8(output.stderr)?,
+ ))
+}
+
#[derive(Debug, Default)]
pub struct CryptoDevice {
fs_type: String,
@@ -35,8 +103,6 @@ pub struct CryptoDevice {
uuid: String,
fs_ver: String,
mountpoints: Option<String>,
- sudo_password: Option<String>,
- luks_passphrase: Option<String>,
}
impl CryptoDevice {
@@ -63,8 +129,6 @@ impl CryptoDevice {
.remove(0)
.ok_or_else(|| FmError::custom("CryptoDevice", "parameter shouldn't be None"))?,
mountpoints: params.remove(0),
- sudo_password: None,
- luks_passphrase: None,
})
}
@@ -72,9 +136,8 @@ impl CryptoDevice {
self.mountpoints.is_some()
}
- pub fn format_luksopen_parameters(&self) -> [String; 5] {
+ fn format_luksopen_parameters(&self) -> [String; 4] {
[
- "-S".to_owned(),
"cryptsetup".to_owned(),
"luksOpen".to_owned(),
self.path.clone(),
@@ -82,19 +145,17 @@ impl CryptoDevice {
]
}
- pub fn format_mkdir_parameters(&self, username: &str) -> [String; 4] {
+ fn format_mkdir_parameters(&self, username: &str) -> [String; 3] {
[
- "-S".to_owned(),
"mkdir".to_owned(),
"-p".to_owned(),
format!("/run/media/{}/{}", username, self.uuid),
]
}
- pub fn format_mount_parameters(&self, username: &str) -> [String; 6] {
+ fn format_mount_parameters(&self, username: &str) -> [String; 5] {
[
- "-S".to_owned(),
- "cryptsetup".to_owned(),
+ "mount".to_owned(),
"-t".to_owned(),
"ext4".to_owned(), // TODO! other fs ???
format!("/dev/mapper/{}", self.uuid),
@@ -102,86 +163,37 @@ impl CryptoDevice {
]
}
- pub fn format_umount_parameters(&self, username: &str) -> [String; 1] {
- [format!("/run/media/mapper/{}/{}", username, self.uuid)]
- }
-
- pub fn format_luksclose_parameters(&self) -> [String; 1] {
- [self.uuid.to_owned()]
- }
-
- pub fn set_sudo_password(&mut self, password: &str) {
- self.sudo_password = Some(password.to_owned())
+ fn format_umount_parameters(&self, username: &str) -> [String; 2] {
+ [
+ "umount".to_owned(),
+ format!("/run/media/mapper/{}/{}", username, self.uuid),
+ ]
}
- pub fn set_luks_passphrase(&mut self, passphrase: &str) {
- self.luks_passphrase = Some(passphrase.to_owned())
+ fn format_luksclose_parameters(&self) -> [String; 3] {
+ [
+ "cryptsetup".to_owned(),
+ "luksClose".to_owned(),
+ self.uuid.to_owned(),
+ ]
}
- pub fn open_mount(&self, username: &str) -> FmResult<()> {
+ pub fn open_mount(&self, username: &str, passwords: &PasswordHolder) -> FmResult<()> {
if self.is_already_mounted() {
Err(FmError::custom(
"luks open mount",
"device is already mounted",
))
- } else if let Some(password) = &self.sudo_password {
- if let Some(passphrase) = &self.luks_passphrase {
- let password = password.to_owned();
- let password2 = password.clone();
- let passphrase = passphrase.to_owned();
- let passphrase2 = passphrase.clone();
-
- let mut child = Command::new("sudo")
- .args(&self.format_luksopen_parameters())
- .stdin(Stdio::piped())
- .stdout(Stdio::piped())
- .spawn()?;
- let mut stdin = child.stdin.take().expect("Failed to open stdin");
- std::thread::spawn(move || {
- stdin
- .write_all(format!("{}\n{}", &password, &passphrase).as_bytes())
- .expect("Failed to write to stdin");
- });
- child.wait_with_output()?;
-
- let mut child = Command::new("sudo")
- .args(&self.format_mkdir_parameters(username))
- .stdin(Stdio::piped())
- .stdout(Stdio::piped())
- .spawn()?;
- let mut stdin = child.stdin.take().expect("Failed to open stdin");
- std::thread::spawn(move || {
- stdin
- .write_all(password2.as_bytes())
- .expect("Failed to write to stdin");
- });
- child.wait_with_output()?;
-
- let mut child = Command::new("sudo")
- .args(&self.format_mount_parameters(username))
- .stdin(Stdio::piped())
- .stdout(Stdio::piped())
- .spawn()?;
- let mut stdin = child.stdin.take().expect("Failed to open stdin");
- std::thread::spawn(move || {
- stdin
- .write_all(passphrase2.as_bytes())
- .expect("Failed to write to stdin");
- });
- child.wait_with_output()?;
-
- Ok(())
- } else {
- Err(FmError::custom(
- "luks open mount",
- "missing a password or passphrase",
- ))
- }
} else {
- Err(FmError::custom(
- "luks open mount",
- "missing a password or passphrase",
- ))
+ // sudo
+ run_privileged_command(&["-S".to_owned(), "ls".to_owned()], &passwords.sudo()?)?;
+ // open
+ run_privileged_command(&self.format_luksopen_parameters(), &passwords.cryptsetup()?)?;
+ // mkdir
+ run_command("sudo", &self.format_mkdir_parameters(username))?;
+ // mount
+ run_command("sudo", &self.format_mount_parameters(username))?;
+ Ok(())
}
}
}
diff --git a/src/main.rs b/src/main.rs
index 71bf208..62d0446 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
use std::sync::Arc;
use clap::Parser;
-use fm::luks::CryptoDevice;
+use fm::luks::{CryptoDevice, PasswordHolder};
use log::info;
use fm::args::Args;
@@ -51,17 +51,60 @@ fn main2() -> FmResult<()> {
}
fn main() -> FmResult<()> {
+ // use std::io::Write;
+ // use std::process::{Command, Stdio};
+
use fm::luks::{filter_crypto_devices_lines, get_devices};
let ret_val = get_devices()?;
println!("{:?}", ret_val);
let output = filter_crypto_devices_lines(ret_val);
println!("{:?}", output);
- let mut crypto_device = CryptoDevice::from_line(&output[0])?;
- crypto_device.set_sudo_password("123");
- crypto_device.set_luks_passphrase("123");
+ let crypto_device = CryptoDevice::from_line(&output[0])?;
+ let mut password_holder = PasswordHolder::default();
+ password_holder.set_sudo_password("aze");
+ password_holder.set_cryptsetup_password("aze");
println!("{:?}", crypto_device);
- crypto_device.open_mount("quentin")?;
+ crypto_device.open_mount("quentin", &password_holder)?;
+ // std::env::set_var("SUDO_ASKPASS", "/usr/lib/ssh/ssh-askpass");
+ // let mut child = Command::new("sudo")
+ // .args(&["-S", "ls"])
+ // .stdin(Stdio::piped())
+ // .stdout(Stdio::piped())
+ // .stderr(Stdio::piped())
+ // .spawn()?;
+ // let stdin = child.stdin.as_mut().expect("Failed to open stdin");
+ // stdin
+ // .write_all("aze\n".as_bytes())
+ // .expect("Failed to write to stdin");
+ // drop(stdin);
+ // let output = child.wait_with_output()?;
+ // println!(
+ // "status {:?} out {:?} err {:?}",
+ // output.status,
+ // String::from_utf8_lossy(&output.stdout),
+ // String::from_utf8_lossy(&output.stderr)
+ // );
+ //
+ // let mut child = Command::new("sudo")
+ // .args(&["cryptsetup", "luksOpen", "/dev/sdb", "test_luks"])
+ // .stdin(Stdio::piped())
+ // .stdout(Stdio::piped())
+ // .stderr(Stdio::piped())
+ // .spawn()?;
+ // let stdin = child.stdin.as_mut().expect("Failed to open stdin");
+ // stdin
+ // .write_all("aze\n".as_bytes())
+ // .expect("Failed to write to stdin");
+ // drop(stdin);
+ //
+ // let output = child.wait_with_output()?;
+ // println!(
+ // "status {:?} out {:?} err {:?}",
+ // output.status,
+ // String::from_utf8_lossy(&output.stdout),
+ // String::from_utf8_lossy(&output.stderr)
+ // );
Ok(())
}