fix(credentials): run sudo without a password when the account has none
A passwordless sudo setup (NOPASSWD, or an account without a password) was still asked for a password: leaving the field empty never satisfied the gate, so the dialog reopened and the operation could not run at all. The gate now asks sudo itself (`sudo -n true`) and lets the request through when it needs nothing, privileged commands fall back to `sudo -n` when no password is held, and an empty field cancels instead of looping. Releasing the lock records its own pending request so an answered prompt is replayed rather than lost.
This commit is contained in:
9 files changed
+131
-24
No files matched your search
@@ -72,10 +72,11 @@ static std::string lastOutputLines(const std::string& output, int keep = 5) {
|
||||
}
|
||||
|
||||
// Run a privileged pacman command as root, piping the password to `sudo -S`
|
||||
// on stdin and capturing combined stdout+stderr. Output is delivered to the
|
||||
// progress callback as pacman produces it — including its in-place download
|
||||
// meters, which is what makes the bar move during a long download — and the
|
||||
// full output is still returned for the auth/error checks.
|
||||
// on stdin — or using `sudo -n` when there is none — and capturing combined
|
||||
// stdout+stderr. Output is delivered to the progress callback as pacman
|
||||
// produces it — including its in-place download meters, which is what makes
|
||||
// the bar move during a long download — and the full output is still returned
|
||||
// for the auth/error checks.
|
||||
//
|
||||
// The percentage comes from ProgressTracker, not from the raw counters: pacman
|
||||
// restarts "(1/24)" at every phase of the transaction, so forwarding those
|
||||
@@ -86,9 +87,9 @@ static std::string execPrivileged(const std::string& pacmanCmd,
|
||||
// The journal gets the command as the user would type it. The password is
|
||||
// piped to sudo on stdin and must never reach the trace.
|
||||
trace("$ sudo " + pacmanCmd);
|
||||
const std::string full = "printf '%s\\n' " + shellQuote(password)
|
||||
+ " | sudo -S " + pacmanCmd + " 2>&1";
|
||||
return runWithProgress(full, progressCallback);
|
||||
return runWithProgress(PacmanManager::privilegedCommand(pacmanCmd, password)
|
||||
+ " 2>&1",
|
||||
progressCallback);
|
||||
}
|
||||
|
||||
// pacman's database lock file. Every manager that goes through libalpm (pacman,
|
||||
@@ -766,10 +767,8 @@ bool PacmanManager::waitForDatabase(int timeoutMs) {
|
||||
}
|
||||
|
||||
int PacmanManager::runCommand(const std::string& command, bool withSudo) const {
|
||||
std::string full = command;
|
||||
if (withSudo) {
|
||||
full = "printf '%s\\n' " + shellQuote(m_password) + " | sudo -S " + command;
|
||||
}
|
||||
const std::string full = withSudo ? privilegedCommand(command, m_password)
|
||||
: command;
|
||||
return system((full + " > /dev/null 2>&1").c_str());
|
||||
}
|
||||
|
||||
@@ -778,15 +777,15 @@ bool PacmanManager::terminateProcess(int pid, const char* signal) const {
|
||||
"kill -" + std::string(signal) + " " + std::to_string(pid);
|
||||
if (runCommand(command, /*withSudo=*/false) == 0) return true;
|
||||
// The holder belongs to another user (a manager started with sudo in a
|
||||
// terminal): only the session password can reach it.
|
||||
return hasPassword() && runCommand(command, /*withSudo=*/true) == 0;
|
||||
// terminal): only root can reach it. Called unconditionally: without a
|
||||
// password this is `sudo -n`, which fails at once when one is required.
|
||||
return runCommand(command, /*withSudo=*/true) == 0;
|
||||
}
|
||||
|
||||
bool PacmanManager::removeLockFile() const {
|
||||
const std::string command = std::string("rm -f ") + kLockFilePath;
|
||||
if (runCommand(command, /*withSudo=*/false) == 0 && databaseIsFree()) return true;
|
||||
return hasPassword() && runCommand(command, /*withSudo=*/true) == 0
|
||||
&& databaseIsFree();
|
||||
return runCommand(command, /*withSudo=*/true) == 0 && databaseIsFree();
|
||||
}
|
||||
|
||||
bool PacmanManager::releaseDatabaseLock(const LockHolder& holder,
|
||||
@@ -866,6 +865,21 @@ void PacmanManager::clearPassword() {
|
||||
m_password.clear();
|
||||
}
|
||||
|
||||
bool PacmanManager::sudoWorksWithoutPassword() {
|
||||
return system("sudo -n true > /dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
std::string PacmanManager::privilegedCommand(const std::string& command,
|
||||
const std::string& password) {
|
||||
// No password is not a missing one: it means the account may use a
|
||||
// passwordless sudo, which `-n` runs without asking. On a setup that does
|
||||
// require a password, `-n` fails at once ("a password is required"), which
|
||||
// authFailed() reports as an authentication error instead of hanging on a
|
||||
// prompt the command cannot answer.
|
||||
if (password.empty()) return "sudo -n " + command;
|
||||
return "printf '%s\\n' " + shellQuote(password) + " | sudo -S " + command;
|
||||
}
|
||||
|
||||
bool PacmanManager::hasPassword() const {
|
||||
return !m_password.empty();
|
||||
}
|
||||
Reference in new issue
Block a user