fix add password manager
This commit is contained in:
137
cpp/managers/passwordmanager/passwordmanager.cpp
Normal file
137
cpp/managers/passwordmanager/passwordmanager.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
#include "passwordmanager.hpp"
|
||||
#include <QFile>
|
||||
#include <QProcessEnvironment>
|
||||
#include <QStandardPaths>
|
||||
|
||||
PasswordManager::PasswordManager(QObject* parent) : QObject{parent}
|
||||
{
|
||||
connect(&m_check, &QProcess::finished, this, &PasswordManager::on_check_finished);
|
||||
connect(&m_check, &QProcess::errorOccurred, this, &PasswordManager::on_check_error);
|
||||
}
|
||||
|
||||
bool PasswordManager::password_empty() const { return m_password.isEmpty(); }
|
||||
|
||||
bool PasswordManager::check_running() const { return m_check_running; }
|
||||
|
||||
void PasswordManager::check_password(const QString& password)
|
||||
{
|
||||
if (m_check_running)
|
||||
{
|
||||
emit password_rejected(QStringLiteral("Проверка пароля уже идёт"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.isEmpty())
|
||||
{
|
||||
emit password_rejected(QStringLiteral("Пароль пустой"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QString sudo = QStandardPaths::findExecutable(QStringLiteral("sudo"));
|
||||
if (sudo.isEmpty())
|
||||
{
|
||||
emit password_rejected(QStringLiteral("sudo не найден"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QString askpass = write_askpass_helper(password);
|
||||
if (askpass.isEmpty())
|
||||
{
|
||||
emit password_rejected(QStringLiteral("Не удалось подготовить проверку пароля"));
|
||||
return;
|
||||
}
|
||||
|
||||
// -v только проверяет пароль, команду не запускает;
|
||||
// -k не даёт sudo поверить кэшу, иначе неверный пароль прошёл бы.
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
env.insert(QStringLiteral("SUDO_ASKPASS"), askpass);
|
||||
env.insert(QStringLiteral("SUDO_ASKPASS_REQUIRE"), QStringLiteral("force"));
|
||||
env.insert(QStringLiteral("LC_ALL"), QStringLiteral("C"));
|
||||
|
||||
m_check.setProcessEnvironment(env);
|
||||
m_candidate = password;
|
||||
m_check_running = true;
|
||||
m_check.start(sudo, {QStringLiteral("-k"), QStringLiteral("-A"), QStringLiteral("-v")});
|
||||
}
|
||||
|
||||
void PasswordManager::on_check_finished(int exit_code, QProcess::ExitStatus status)
|
||||
{
|
||||
if (!m_check_running) return;
|
||||
m_check_running = false;
|
||||
|
||||
const QString stderr_text = QString::fromLocal8Bit(m_check.readAllStandardError()).trimmed();
|
||||
m_askpass_dir.reset();
|
||||
|
||||
if (status == QProcess::NormalExit && exit_code == 0)
|
||||
{
|
||||
// пароль проверен — запоминаем до конца работы программы
|
||||
m_password = m_candidate;
|
||||
m_candidate.clear();
|
||||
emit password_accepted();
|
||||
return;
|
||||
}
|
||||
|
||||
m_candidate.clear();
|
||||
qWarning().noquote() << "password check failed:" << stderr_text;
|
||||
|
||||
if (stderr_text.contains(QStringLiteral("incorrect password"), Qt::CaseInsensitive)
|
||||
|| stderr_text.contains(QStringLiteral("Sorry, try again"), Qt::CaseInsensitive)
|
||||
|| stderr_text.contains(QStringLiteral("authentication failure"), Qt::CaseInsensitive))
|
||||
emit password_rejected(QStringLiteral("Неверный пароль, попробуйте ещё раз"));
|
||||
else
|
||||
emit password_rejected(QStringLiteral("Не удалось проверить пароль"));
|
||||
}
|
||||
|
||||
void PasswordManager::on_check_error(QProcess::ProcessError error)
|
||||
{
|
||||
if (error != QProcess::FailedToStart) return;
|
||||
if (!m_check_running) return;
|
||||
|
||||
m_check_running = false;
|
||||
m_candidate.clear();
|
||||
m_askpass_dir.reset();
|
||||
|
||||
qWarning() << "cannot start sudo for password check:" << m_check.errorString();
|
||||
emit password_rejected(QStringLiteral("Не удалось запустить sudo"));
|
||||
}
|
||||
|
||||
QString PasswordManager::askpass_path()
|
||||
{
|
||||
if (m_password.isEmpty()) return {};
|
||||
return write_askpass_helper(m_password);
|
||||
}
|
||||
|
||||
void PasswordManager::clear_askpass()
|
||||
{
|
||||
m_askpass_dir.reset();
|
||||
}
|
||||
|
||||
QString PasswordManager::write_askpass_helper(const QString& password)
|
||||
{
|
||||
m_askpass_dir = std::make_unique<QTemporaryDir>();
|
||||
if (!m_askpass_dir->isValid())
|
||||
{
|
||||
qWarning() << "cannot create temporary dir for askpass";
|
||||
m_askpass_dir.reset();
|
||||
return {};
|
||||
}
|
||||
|
||||
QFile::setPermissions(m_askpass_dir->path(), QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
|
||||
const QString path = m_askpass_dir->filePath(QStringLiteral("askpass.sh"));
|
||||
QFile script(path);
|
||||
if (!script.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
||||
{
|
||||
qWarning() << "cannot write askpass helper:" << script.errorString();
|
||||
m_askpass_dir.reset();
|
||||
return {};
|
||||
}
|
||||
|
||||
QString escaped = password;
|
||||
escaped.replace(QLatin1Char('\''), QStringLiteral("'\\''"));
|
||||
|
||||
script.write("#!/bin/sh\nprintf '%s\\n' '" + escaped.toUtf8() + "'\n");
|
||||
script.close();
|
||||
script.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
|
||||
|
||||
return path;
|
||||
}
|
||||
38
cpp/managers/passwordmanager/passwordmanager.hpp
Normal file
38
cpp/managers/passwordmanager/passwordmanager.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QTemporaryDir>
|
||||
#include <memory>
|
||||
|
||||
class PasswordManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PasswordManager(QObject* parent = nullptr);
|
||||
|
||||
public:
|
||||
bool password_empty() const;
|
||||
bool check_running() const;
|
||||
void check_password(const QString& password);
|
||||
QString askpass_path();
|
||||
void clear_askpass();
|
||||
|
||||
private:
|
||||
QString write_askpass_helper(const QString& password);
|
||||
void on_check_finished(int exit_code, QProcess::ExitStatus status);
|
||||
void on_check_error(QProcess::ProcessError error);
|
||||
|
||||
private:
|
||||
QString m_password;
|
||||
QString m_candidate;
|
||||
std::unique_ptr<QTemporaryDir> m_askpass_dir;
|
||||
QProcess m_check;
|
||||
bool m_check_running = false;
|
||||
|
||||
signals:
|
||||
void password_accepted();
|
||||
void password_rejected(const QString& reason);
|
||||
};
|
||||
Reference in New Issue
Block a user