feat(session): clear the one-shot autologin when a launch is killed
A launch stopped before its cleanup left the drop-in armed, so the next boot skipped the password prompt without anyone noticing. Mark what the tool writes, create a transient unit whose ExecStopPost removes it on stop, timeout and shutdown, and clean up on SIGTERM/SIGINT/SIGHUP in the helper.
This commit is contained in:
3 files changed
+238
-11
No files matched your search
@@ -826,5 +826,10 @@ int main(int argc, char** argv) {
|
||||
if (argc == 4 && std::string(argv[1]) == "--privileged-launch") {
|
||||
return fws::runLaunchHelper(argv[2], argv[3]);
|
||||
}
|
||||
// Same re-entry for removing a drop-in, which the watchdog unit runs as root
|
||||
// when it notices a launch that never cleaned up.
|
||||
if (argc == 2 && std::string(argv[1]) == "--privileged-cleanup") {
|
||||
return fws::runAutologinCleanup();
|
||||
}
|
||||
return ManagerWindow::run(argc, argv);
|
||||
}
|
||||
+225
-11
@@ -11,6 +11,7 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -29,6 +30,41 @@ constexpr int kLoginTimeoutMs = 90000;
|
||||
constexpr const char* kHelperName = "fws-deskmanager";
|
||||
// Hidden switch that turns the single binary into the privileged helper.
|
||||
constexpr const char* kHelperMode = "--privileged-launch";
|
||||
// Hidden switch for the privileged removal of a drop-in. The watchdog unit calls
|
||||
// it on its own, so it has to work with no other argument.
|
||||
constexpr const char* kCleanupMode = "--privileged-cleanup";
|
||||
|
||||
// One-shot autologin drop-ins this tool owns. Display managers read a directory
|
||||
// rather than a single file, so a launch adds a file of its own instead of
|
||||
// editing the configuration the administrator maintains.
|
||||
constexpr const char* kSddmDropIn = "/etc/sddm.conf.d/zz-fws-autologin.conf";
|
||||
constexpr const char* kLightdmDropIn = "/etc/lightdm/lightdm.conf.d/99-fws-autologin.conf";
|
||||
|
||||
// First line of every drop-in. A launch killed before its cleanup leaves the
|
||||
// file behind, and this marker is what lets the tool delete it later without
|
||||
// ever touching a configuration it did not write.
|
||||
constexpr const char* kDropInMarker =
|
||||
"# FWS-DeskManager one-shot autologin - removed automatically";
|
||||
|
||||
// A killed launch must not keep the autologin armed: the transient unit started
|
||||
// alongside the drop-in deletes it when stopped, on timeout, and while the
|
||||
// machine shuts down.
|
||||
constexpr int kWatchdogSeconds = 180;
|
||||
|
||||
// The helper waits up to ninety seconds for the seat: a terminated launch still
|
||||
// has to reach its cleanup instead of dying inside the sleep.
|
||||
volatile std::sig_atomic_t g_interrupted = 0;
|
||||
|
||||
void onTerminationSignal(int) { g_interrupted = 1; }
|
||||
|
||||
void catchTerminationSignals() {
|
||||
struct sigaction action {};
|
||||
action.sa_handler = onTerminationSignal;
|
||||
sigemptyset(&action.sa_mask);
|
||||
sigaction(SIGTERM, &action, nullptr);
|
||||
sigaction(SIGINT, &action, nullptr);
|
||||
sigaction(SIGHUP, &action, nullptr);
|
||||
}
|
||||
|
||||
std::string lower(std::string value) {
|
||||
std::transform(value.begin(), value.end(), value.begin(),
|
||||
@@ -61,14 +97,15 @@ bool autologinFile(const std::string& unit, const std::string& user,
|
||||
const std::string& sessionFile, std::filesystem::path& path,
|
||||
std::string& content) {
|
||||
if (unit == "sddm.service") {
|
||||
path = "/etc/sddm.conf.d/zz-fws-autologin.conf";
|
||||
content = "[Autologin]\nUser=" + user + "\nSession=" + sessionFile + "\nRelogin=false\n";
|
||||
path = kSddmDropIn;
|
||||
content = std::string(kDropInMarker) + "\n[Autologin]\nUser=" + user +
|
||||
"\nSession=" + sessionFile + "\nRelogin=false\n";
|
||||
return true;
|
||||
}
|
||||
if (unit == "lightdm.service") {
|
||||
path = "/etc/lightdm/lightdm.conf.d/99-fws-autologin.conf";
|
||||
content = "[Seat:*]\nautologin-user=" + user + "\nautologin-session=" + sessionFile +
|
||||
"\nautologin-user-timeout=0\n";
|
||||
path = kLightdmDropIn;
|
||||
content = std::string(kDropInMarker) + "\n[Seat:*]\nautologin-user=" + user +
|
||||
"\nautologin-session=" + sessionFile + "\nautologin-user-timeout=0\n";
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -104,6 +141,73 @@ bool writeTextFile(const std::filesystem::path& path, const std::string& content
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::string> dropInPaths() { return {kSddmDropIn, kLightdmDropIn}; }
|
||||
|
||||
bool carriesMarker(const std::string& content) {
|
||||
return content.find(kDropInMarker) != std::string::npos;
|
||||
}
|
||||
|
||||
// Drop-ins written by builds that had no marker are still ours when they hold
|
||||
// nothing but the keys this tool writes: one launch or `cleanup` clears them.
|
||||
bool onlyOwnedKeys(const std::string& content) {
|
||||
static const std::vector<std::string> kOwnedKeys = {
|
||||
"[autologin]", "[seat:*]", "user=",
|
||||
"session=", "relogin=", "autologin-user=",
|
||||
"autologin-session=", "autologin-user-timeout="};
|
||||
for (const std::string& line : splitLines(content)) {
|
||||
if (line[0] == '#' || line[0] == ';') {
|
||||
continue;
|
||||
}
|
||||
std::string key = lower(line);
|
||||
const std::size_t equals = key.find('=');
|
||||
if (equals != std::string::npos) {
|
||||
key = key.substr(0, equals + 1);
|
||||
}
|
||||
if (std::find(kOwnedKeys.begin(), kOwnedKeys.end(), key) == kOwnedKeys.end()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ownsDropIn(const std::filesystem::path& path) {
|
||||
std::error_code code;
|
||||
if (!std::filesystem::is_regular_file(path, code)) {
|
||||
return false;
|
||||
}
|
||||
const std::string content = readTextFile(path);
|
||||
if (content.empty()) {
|
||||
return false;
|
||||
}
|
||||
return carriesMarker(content) || onlyOwnedKeys(content);
|
||||
}
|
||||
|
||||
std::string firstOwnedDropIn() {
|
||||
for (const std::string& path : dropInPaths()) {
|
||||
if (ownsDropIn(path)) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
bool removeOwnedDropIns(std::vector<std::string>& removed, std::string& error) {
|
||||
bool ok = true;
|
||||
for (const std::string& path : dropInPaths()) {
|
||||
if (!ownsDropIn(path)) {
|
||||
continue;
|
||||
}
|
||||
std::error_code code;
|
||||
if (std::filesystem::remove(path, code)) {
|
||||
removed.push_back(path);
|
||||
} else {
|
||||
ok = false;
|
||||
error = "Impossible de supprimer " + path + " : " + code.message();
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::string currentUserName() {
|
||||
const passwd* account = getpwuid(getuid());
|
||||
return account != nullptr ? std::string(account->pw_name) : std::string();
|
||||
@@ -124,6 +228,45 @@ std::string launchHelperPath() {
|
||||
return findExecutable(kHelperName);
|
||||
}
|
||||
|
||||
// The drop-in is only safe if something outside the helper can delete it. The
|
||||
// unit below lives for the whole launch and its ExecStopPost fires when the unit
|
||||
// is stopped by us, when RuntimeMaxSec expires after a killed helper, and when
|
||||
// systemd shuts the machine down.
|
||||
std::string startCleanupWatchdog(const std::string& helper, std::string& error) {
|
||||
if (helper.empty()) {
|
||||
error = "Le nettoyage de l'autologin n'a pas de binaire a appeler";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string stopCommand = helper;
|
||||
if (stopCommand.find(' ') != std::string::npos) {
|
||||
stopCommand = "\"" + stopCommand + "\"";
|
||||
}
|
||||
stopCommand += std::string(" ") + kCleanupMode;
|
||||
|
||||
const std::string unit = "fws-deskmanager-autologin-" + std::to_string(getpid());
|
||||
const CommandResult result = runCommand(
|
||||
{"systemd-run", "--quiet", "--collect", "--unit=" + unit,
|
||||
"--property=RuntimeMaxSec=" + std::to_string(kWatchdogSeconds),
|
||||
"--property=ExecStopPost=" + stopCommand, "/bin/sleep",
|
||||
std::to_string(kWatchdogSeconds)});
|
||||
if (!result.started || result.exitCode != 0) {
|
||||
const std::string detail = trim(result.output);
|
||||
error = "Nettoyage automatique de l'autologin impossible (systemd-run)";
|
||||
if (!detail.empty()) {
|
||||
error += " : " + detail;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
|
||||
void stopCleanupWatchdog(const std::string& unit) {
|
||||
if (!unit.empty()) {
|
||||
runCommand({"systemctl", "stop", unit});
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> desktopNamesFor(const std::string& sessionFile) {
|
||||
std::vector<std::string> names;
|
||||
for (const DesktopEntry& entry : listInstalledDesktops()) {
|
||||
@@ -237,6 +380,26 @@ bool canStartWithoutGreeter(std::string& reason) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string staleAutologinFile() { return firstOwnedDropIn(); }
|
||||
|
||||
int runAutologinCleanup() {
|
||||
if (geteuid() != 0) {
|
||||
std::cerr << "Cette commande doit etre lancee en root (pkexec).\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<std::string> removed;
|
||||
std::string error;
|
||||
if (!removeOwnedDropIns(removed, error)) {
|
||||
std::cerr << error << "\n";
|
||||
return 1;
|
||||
}
|
||||
for (const std::string& path : removed) {
|
||||
std::cout << "Autologin residuel supprime : " << path << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
LaunchOutcome launchDesktop(const DesktopProfile& profile) {
|
||||
LaunchOutcome outcome;
|
||||
|
||||
@@ -298,6 +461,11 @@ LaunchOutcome launchDesktop(const DesktopProfile& profile) {
|
||||
const std::string message = trim(result.output);
|
||||
outcome.error = message.empty() ? "Le demarrage de " + profile.name + " a echoue."
|
||||
: message;
|
||||
if (!firstOwnedDropIn().empty()) {
|
||||
outcome.error +=
|
||||
" Un autologin residuel subsiste ; l'unite de nettoyage le retirera, au plus "
|
||||
"tard a l'extinction.";
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
|
||||
@@ -322,6 +490,18 @@ int runLaunchHelper(const std::string& user, const std::string& sessionFile) {
|
||||
}
|
||||
const std::string uid = std::to_string(account->pw_uid);
|
||||
|
||||
// A previous launch may have been killed before its cleanup: start from a
|
||||
// clean state, so the drop-in written below is the only one in place.
|
||||
std::vector<std::string> staleFiles;
|
||||
std::string staleError;
|
||||
if (!removeOwnedDropIns(staleFiles, staleError)) {
|
||||
std::cerr << staleError << "\n";
|
||||
return 3;
|
||||
}
|
||||
for (const std::string& stale : staleFiles) {
|
||||
std::cout << "Autologin residuel supprime : " << stale << "\n";
|
||||
}
|
||||
|
||||
std::filesystem::path configPath;
|
||||
std::string content;
|
||||
if (!autologinFile(displayManagerUnit(), user, sessionFile, configPath, content)) {
|
||||
@@ -344,9 +524,24 @@ int runLaunchHelper(const std::string& user, const std::string& sessionFile) {
|
||||
std::error_code code;
|
||||
const bool existed = std::filesystem::exists(configPath, code);
|
||||
const std::string backup = existed ? readTextFile(configPath) : std::string();
|
||||
// What an earlier launch left at this path is removed, never restored.
|
||||
const bool staleBackup = existed && ownsDropIn(configPath);
|
||||
|
||||
// Never arm an autologin that nothing would disarm: without the cleanup unit
|
||||
// the launch is refused instead of leaving a password-free boot behind.
|
||||
std::string watchdogError;
|
||||
const std::string watchdog = startCleanupWatchdog(launchHelperPath(), watchdogError);
|
||||
if (watchdog.empty()) {
|
||||
std::cerr << watchdogError << ". Lancement annule pour ne pas laisser "
|
||||
<< configPath.string() << " en place.\n";
|
||||
return 3;
|
||||
}
|
||||
|
||||
catchTerminationSignals();
|
||||
|
||||
std::string error;
|
||||
if (!writeTextFile(configPath, content, error)) {
|
||||
stopCleanupWatchdog(watchdog);
|
||||
std::cerr << error << "\n";
|
||||
return 3;
|
||||
}
|
||||
@@ -363,7 +558,7 @@ int runLaunchHelper(const std::string& user, const std::string& sessionFile) {
|
||||
std::string unexpected;
|
||||
const auto deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(kLoginTimeoutMs);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
while (std::chrono::steady_clock::now() < deadline && g_interrupted == 0) {
|
||||
for (const SessionInfo& session : listGraphicalSessionsForUid(uid)) {
|
||||
if (!session.active ||
|
||||
std::find(previous.begin(), previous.end(), session.id) != previous.end()) {
|
||||
@@ -383,11 +578,30 @@ int runLaunchHelper(const std::string& user, const std::string& sessionFile) {
|
||||
}
|
||||
|
||||
// The autologin only exists for this launch: a later boot must show the
|
||||
// greeter instead of logging the user in on its own.
|
||||
if (existed) {
|
||||
writeTextFile(configPath, backup, error);
|
||||
} else {
|
||||
std::filesystem::remove(configPath, code);
|
||||
// greeter instead of logging the user in on its own. Stopping the cleanup
|
||||
// unit already deletes the drop-in; removing it here as well keeps the
|
||||
// helper independent from systemd having kept the unit alive.
|
||||
stopCleanupWatchdog(watchdog);
|
||||
std::filesystem::remove(configPath, code);
|
||||
if (existed && !staleBackup) {
|
||||
std::string restoreError;
|
||||
if (!writeTextFile(configPath, backup, restoreError)) {
|
||||
std::cerr << restoreError << "\n";
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
if (ownsDropIn(configPath)) {
|
||||
std::cerr << "L'autologin " << configPath.string()
|
||||
<< " n'a pas pu etre retire ; supprimez-le avant le prochain demarrage.\n";
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (g_interrupted != 0) {
|
||||
std::cerr << "Lancement interrompu ; l'autologin a ete retire.\n";
|
||||
if (!previousActive.empty()) {
|
||||
runCommand({"loginctl", "activate", previousActive});
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (sessionId.empty()) {
|
||||
|
||||
@@ -23,6 +23,14 @@ bool canStartWithoutGreeter(std::string& reason);
|
||||
// helper (polkit) and closes the sessions it replaces.
|
||||
LaunchOutcome launchDesktop(const DesktopProfile& profile);
|
||||
|
||||
// Drop-in left behind by a launch killed before its cleanup, empty when the
|
||||
// system is clean. Readable without privileges so the window can warn about it.
|
||||
std::string staleAutologinFile();
|
||||
|
||||
// Privileged side, run by the same binary with --privileged-cleanup as root:
|
||||
// deletes every drop-in this tool owns. Returns 0 when nothing is left behind.
|
||||
int runAutologinCleanup();
|
||||
|
||||
// Privileged side, run by the same binary with --privileged-launch as root.
|
||||
// Returns 0 when the requested session took the seat and the replaced sessions
|
||||
// are closing.
|
||||
|
||||
Reference in new issue
Block a user