Feat: Implementiert Funktionen zum Überprüfen und Neustarten mit Root-/Admin-Rechten

This commit is contained in:
2025-08-21 00:07:22 +02:00
parent da371e5b85
commit 4373b60193
3 changed files with 61 additions and 0 deletions

View File

@@ -8,3 +8,4 @@ authors = ['DragonSlayer_14']
time = { version="0.3.41", features = ["formatting", "macros", "local-offset"] } time = { version="0.3.41", features = ["formatting", "macros", "local-offset"] }
serde = { version="1.0.219", features = ["derive"] } serde = { version="1.0.219", features = ["derive"] }
confy = "1.0.0" confy = "1.0.0"
libc = "1.0.0-alpha.1"

View File

@@ -15,6 +15,7 @@ mod network;
mod config; mod config;
mod program; mod program;
mod filesystem; mod filesystem;
mod sudo;
fn main() { fn main() {
log("main", "========== PROGRAM START ==========", LogLevel::Info); log("main", "========== PROGRAM START ==========", LogLevel::Info);

59
src/sudo.rs Normal file
View File

@@ -0,0 +1,59 @@
/// Prüft, ob das Programm mit Root-/Administrator-Rechten ausgeführt wird.
///
/// # Returns
///
/// * `true` - Das Programm läuft mit erhöhten Rechten
/// * `false` - Das Programm läuft mit normalen Benutzerrechten
pub fn is_run_as_root() -> bool {
#[cfg(any(target_os = "windows"))]
{
// Windows: Prüfen ob Admin-Rechte vorhanden
use std::process::Command;
Command::new("net")
.args(&["session"])
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
#[cfg(not(any(target_os = "windows")))]
{
// Linux/Unix: Prüfen ob Root oder sudo
unsafe { libc::geteuid() == 0 }
}
}
/// Startet das Programm mit Root-/Administrator-Rechten neu.
///
/// Diese Funktion versucht das Programm mit erhöhten Rechten neu zu starten.
///
/// # Details
///
/// - Unter Linux wird das Programm mit `sudo` neu gestartet
/// - Unter Windows wird der UAC-Dialog zur Rechteanfrage angezeigt
pub fn run_as_root() {
use std::process::Command;
#[cfg(any(target_os = "windows"))]
{
use std::os::windows::process::CommandExt;
Command::new("powershell")
.args(&[
"Start-Process",
&std::env::current_exe().unwrap().to_string_lossy(),
"-Verb",
"RunAs"
])
.spawn()
.expect("Fehler beim Neustarten mit Admin-Rechten");
std::process::exit(0);
}
#[cfg(not(any(target_os = "windows")))]
{
Command::new("sudo")
.arg(&std::env::current_exe().unwrap())
.spawn()
.expect("Fehler beim Neustarten mit Root-Rechten");
std::process::exit(0);
}
}