Feat: Implementiert save_credentials_webdav zur Verwaltung von WebDAV-Zugangsdaten für Windows und Unix-Systeme
This commit is contained in:
111
src/filesystem/credentials.rs
Normal file
111
src/filesystem/credentials.rs
Normal file
@@ -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::<Vec<&str>>()
|
||||
.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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user