From d4e7d52afe59992786bd871d1d81d6886c727b04 Mon Sep 17 00:00:00 2001 From: DragonSlayer_14 Date: Thu, 21 Aug 2025 14:01:48 +0200 Subject: [PATCH] =?UTF-8?q?Feat:=20Implementiert=20`save=5Fcredentials=5Fw?= =?UTF-8?q?ebdav`=20zur=20Verwaltung=20von=20WebDAV-Zugangsdaten=20f=C3=BC?= =?UTF-8?q?r=20Windows=20und=20Unix-Systeme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/filesystem/credentials.rs | 111 ++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/filesystem/credentials.rs diff --git a/src/filesystem/credentials.rs b/src/filesystem/credentials.rs new file mode 100644 index 0000000..e40dc35 --- /dev/null +++ b/src/filesystem/credentials.rs @@ -0,0 +1,111 @@ +use crate::config::get_config; +use crate::log::{log, LogLevel}; +use std::process::Command; + +/// Speichert die Zugangsdaten für einen WebDAV-Mount-Punkt. +/// +/// # Parameter +/// * `mount_path` - Der Netzwerkpfad zum WebDAV-Server +/// * `username` - Der Benutzername für den WebDAV-Zugriff +/// * `password` - Das Passwort für den WebDAV-Zugriff +pub fn save_credentials_webdav() { + let config = get_config(); + + #[cfg(target_os = "windows")] + { + let output = Command::new("powershell") + .args([ + "-Command", + &format!("cmdkey /delete:{}", config.remote.mount_path) + ]) + .output() + .expect("Failed to delete credentials"); + + let output = Command::new("powershell") + .args([ + "-Command", + &format!("cmdkey /add:{} /user:{} /pass:{}", + config.remote.mount_path, + config.remote.username, + config.remote.password + ) + ]) + .output() + .expect("Failed to store credentials"); + + if !output.status.success() { + log( + "credentials", + "Could not store credentials in Windows Credential Manager", + LogLevel::Error, + ); + log( + "credentials", + &String::from_utf8_lossy(&output.stderr), + LogLevel::Debug, + ); + } + } + + #[cfg(not(target_os = "windows"))] + { + if config.remote.username.is_empty() || config.remote.password.is_empty() { + log( + "credentials", + "No credentials found in config", + LogLevel::Error, + ); + return; + } + + let output = Command::new("touch") + .arg("/etc/davfs2/secrets") + .output() + .expect("Failed to create secrets file"); + + if !output.status.success() { + log( + "credentials", + "Could not create secrets file", + LogLevel::Error, + ); + return; + } + + let output = Command::new("chmod") + .arg("600") + .arg("/etc/davfs2/secrets") + .output() + .expect("Failed to set secrets file permissions"); + + if !output.status.success() { + log( + "credentials", + "Could not set secrets file permissions", + LogLevel::Error, + ); + return; + } + + let credentials = format!("{} \"{}\" \"{}\"\n", + config.general.mount_point, + config.remote.username, + config.remote.password + ); + let mut content = std::fs::read_to_string("/etc/davfs2/secrets") + .unwrap_or_default(); + + content = content.lines() + .filter(|line| !line.starts_with(&config.general.mount_point)) + .collect::>() + .join("\n"); + + if !content.is_empty() { + content.push('\n'); + } + content.push_str(&credentials); + + std::fs::write("/etc/davfs2/secrets", content) + .expect("Failed to write credentials"); + } +} \ No newline at end of file