From 6702757eaa3bb61d7c44ec4c91cacf8a2e2267b1 Mon Sep 17 00:00:00 2001 From: oupson Date: Tue, 31 May 2022 23:21:50 +0200 Subject: [PATCH] Get options for filesystem --- Cargo.lock | 1 + rustcryptfs-linux/Cargo.toml | 3 +- rustcryptfs-linux/src/encrypted_filesystem.rs | 32 ++++++++++++++----- rustcryptfs-linux/src/error.rs | 18 +++++++++++ rustcryptfs-linux/src/lib.rs | 1 + rustcryptfs/src/main.rs | 2 +- 6 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 rustcryptfs-linux/src/error.rs diff --git a/Cargo.lock b/Cargo.lock index 6d5f73f..787dc4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -558,6 +558,7 @@ dependencies = [ "fuser", "log", "rustcryptfs-lib", + "serde_json", "thiserror", ] diff --git a/rustcryptfs-linux/Cargo.toml b/rustcryptfs-linux/Cargo.toml index 45ed3c3..4f9f09c 100644 --- a/rustcryptfs-linux/Cargo.toml +++ b/rustcryptfs-linux/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" fuser = "0.11" log = "0.4" rustcryptfs-lib = { path = "../rustcryptfs-lib" } -thiserror = "1.0" \ No newline at end of file +thiserror = "1.0" +serde_json = "1.0" \ No newline at end of file diff --git a/rustcryptfs-linux/src/encrypted_filesystem.rs b/rustcryptfs-linux/src/encrypted_filesystem.rs index 29941b4..e2ca24c 100644 --- a/rustcryptfs-linux/src/encrypted_filesystem.rs +++ b/rustcryptfs-linux/src/encrypted_filesystem.rs @@ -1,20 +1,36 @@ -use std::path::Path; +use std::{fs, path::Path}; use fuser::Filesystem; -use rustcryptfs_lib::config::CryptConf; +use rustcryptfs_lib::{config::CryptConf, filename::FilenameDecoder}; -pub struct EncryptedFs {} +use crate::error::Result; + +pub struct EncryptedFs { + master_key: Vec, + filename_decoder: FilenameDecoder, +} impl EncryptedFs { - pub fn new

(path: P) -> Self + pub fn new

(path: P, password: &str) -> Result where P: AsRef, { - todo!() - } + let path = path.as_ref(); - pub fn new_from_config(config: &CryptConf) -> Self { - Self {} + let conf_path = path.join("gocryptfs.conf"); + + let content = fs::read_to_string(conf_path)?; + + let conf: CryptConf = serde_json::from_str(&content)?; + + let master_key = conf.get_master_key(password.as_bytes())?; + + let filename_decoder = FilenameDecoder::new(&master_key)?; + + Ok(Self { + master_key, + filename_decoder, + }) } pub fn mount

(self, mountpoint: P) diff --git a/rustcryptfs-linux/src/error.rs b/rustcryptfs-linux/src/error.rs new file mode 100644 index 0000000..6e51870 --- /dev/null +++ b/rustcryptfs-linux/src/error.rs @@ -0,0 +1,18 @@ +use thiserror::Error; + +pub type Result = std::result::Result; + +#[derive(Debug, Error)] +pub enum Error { + #[error(transparent)] + IoError(#[from] std::io::Error), + + #[error(transparent)] + JsonError(#[from] serde_json::Error), + + #[error(transparent)] + RustCryptFsError(#[from] rustcryptfs_lib::error::Error), + + #[error(transparent)] + RustCryptFsFilenameError(#[from] rustcryptfs_lib::error::FilenameDecryptError), +} diff --git a/rustcryptfs-linux/src/lib.rs b/rustcryptfs-linux/src/lib.rs index 304a802..5d59b1c 100644 --- a/rustcryptfs-linux/src/lib.rs +++ b/rustcryptfs-linux/src/lib.rs @@ -1,3 +1,4 @@ mod encrypted_filesystem; +pub mod error; pub use encrypted_filesystem::EncryptedFs; \ No newline at end of file diff --git a/rustcryptfs/src/main.rs b/rustcryptfs/src/main.rs index 49400bb..86f6c46 100644 --- a/rustcryptfs/src/main.rs +++ b/rustcryptfs/src/main.rs @@ -108,7 +108,7 @@ fn decrypt_file(c: &DecryptCommand) -> anyhow::Result<()> { fn mount(mount: &MountCommand) -> anyhow::Result<()> { use rustcryptfs_linux::EncryptedFs; - let fs = EncryptedFs::new(&mount.path); + let fs = EncryptedFs::new(&mount.path, mount.password.as_ref().unwrap())?; fs.mount(&mount.mountpoint); Ok(())