This commit is contained in:
tuxgyver
2026-07-21 10:21:13 +02:00
parent ea2e6d772b
commit d03a75057e
5 changed files with 187 additions and 80 deletions
+18 -15
View File
@@ -62,10 +62,14 @@ def run_privileged(cmd: list[str], timeout: int = 60) -> tuple[int, str, str]:
if has_root_privileges():
return run_command(cmd, timeout)
# 1. pkexec — dialogue graphique polkit (GNOME/KDE), timeout long pour la saisie
code, out, err = run_command(["pkexec"] + cmd, timeout)
if code != -1 or "introuvable" not in err:
# 1. sudo -n (NOPASSWD configuré dans sudoers — sans aucun dialogue)
code, out, err = run_command(["sudo", "-n"] + cmd, timeout)
if code == 0:
return code, out, err
# Si sudo a tourné mais la commande a échoué (pas un problème d'auth), retourner l'erreur
if code != 1 or ("password" not in err.lower() and "passwd" not in err.lower()):
if "sudo:" not in err.lower() and code not in (-1,):
return code, out, err
# 2. sudo avec programme askpass graphique (pas de TTY dans une app Qt)
askpass = _find_askpass()
@@ -78,24 +82,23 @@ def run_privileged(cmd: list[str], timeout: int = 60) -> tuple[int, str, str]:
capture_output=True, text=True,
timeout=timeout, env=env,
)
return result.returncode, result.stdout.strip(), result.stderr.strip()
except subprocess.TimeoutExpired:
return -1, "", "Timeout élévation sudo"
except Exception as e:
return -1, "", str(e)
if result.returncode == 0:
return result.returncode, result.stdout.strip(), result.stderr.strip()
except (subprocess.TimeoutExpired, Exception):
pass
# 3. sudo classique (fonctionne si NOPASSWD configuré dans sudoers)
code, out, err = run_command(["sudo", "-n"] + cmd, 10)
if code != -1:
# 3. pkexec — dialogue graphique polkit (GNOME/KDE)
code, out, err = run_command(["pkexec"] + cmd, timeout)
if code != -1 or "introuvable" not in err:
return code, out, err
return (
-1, "",
"Élévation de privilèges impossible.\n"
"Solutions :\n"
" • Installer pkexec (polkit) pour le dialogue graphique\n"
" • Ou ajouter dans /etc/sudoers :\n"
f" {os.environ.get('USER','<user>')} ALL=(ALL) NOPASSWD: /usr/bin/wg-quick"
"Exécutez make setup-sudoers pour configurer wg-quick sans dialogue.\n"
"Ou manuellement :\n"
f" sudo bash -c \"echo '{os.environ.get('USER','<user>')} ALL=(ALL) NOPASSWD: /usr/bin/wg-quick'"
" > /etc/sudoers.d/wgsecure && chmod 440 /etc/sudoers.d/wgsecure\""
)