Changeset View
Changeset View
Standalone View
Standalone View
src/util/externalcommand_polkitbackend.cpp
- This file was added.
1 | /************************************************************************* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License as * | ||||
6 | * published by the Free Software Foundation; either version 3 of * | ||||
7 | * the License, or (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>.* | ||||
16 | *************************************************************************/ | ||||
17 | | ||||
18 | #include "util/externalcommand_polkitbackend.h" | ||||
19 | | ||||
20 | #include <QApplication> | ||||
21 | #include <QtDBus> | ||||
22 | #include <QDebug> | ||||
23 | #include <QEventLoop> | ||||
24 | #include <QString> | ||||
25 | #include <QTimer> | ||||
26 | #include <QWidget> | ||||
27 | | ||||
28 | #include <PolkitQt1/Authority> | ||||
29 | #include <PolkitQt1/Subject> | ||||
30 | | ||||
31 | using namespace PolkitQt1; | ||||
32 | | ||||
33 | Authority::Result Auth::PolkitEventLoop::m_result = Authority::No; | ||||
34 | | ||||
35 | namespace Auth | ||||
36 | { | ||||
37 | | ||||
38 | PolkitEventLoop::PolkitEventLoop(QObject *parent) | ||||
39 | : QEventLoop(qobject_cast<QEventLoop *>(parent)) | ||||
40 | { | ||||
41 | | ||||
42 | } | ||||
43 | | ||||
44 | PolkitEventLoop::~PolkitEventLoop() | ||||
45 | { | ||||
46 | | ||||
47 | } | ||||
48 | | ||||
49 | void PolkitEventLoop::requestQuit(const Authority::Result &result) | ||||
50 | { | ||||
51 | m_result = result; | ||||
52 | quit(); | ||||
53 | } | ||||
54 | | ||||
55 | Authority::Result PolkitEventLoop::result() const | ||||
56 | { | ||||
57 | return m_result; | ||||
58 | } | ||||
59 | | ||||
60 | PolkitQt1Backend::PolkitQt1Backend() | ||||
61 | : m_flyingActions(false) | ||||
62 | { | ||||
63 | // Connect various useful Polkit signals | ||||
64 | connect(Authority::instance(), &Authority::configChanged, this, &Auth::PolkitQt1Backend::authStatusChanged); | ||||
65 | connect(Authority::instance(), &Authority::consoleKitDBChanged, this, &Auth::PolkitQt1Backend::authStatusChanged); | ||||
66 | | ||||
67 | m_flyingActions = true; | ||||
68 | PolkitQt1::Authority::instance()->enumerateActions(); | ||||
69 | } | ||||
70 | | ||||
71 | PolkitQt1Backend::~PolkitQt1Backend() | ||||
72 | { | ||||
73 | | ||||
74 | } | ||||
75 | | ||||
76 | void PolkitQt1Backend::startHelper(const QString &action, const QString &helperID) const | ||||
77 | { | ||||
78 | Q_UNUSED(action) | ||||
79 | | ||||
80 | const auto reply = QDBusConnection::systemBus().interface()->startService(helperID); | ||||
81 | | ||||
82 | if (!reply.isValid() && !QDBusConnection::systemBus().interface()->isServiceRegistered(helperID)) { | ||||
83 | qWarning() << QDBusConnection::systemBus().lastError().message(); | ||||
84 | qWarning() << "Failure starting the helper"; | ||||
85 | return; | ||||
86 | } | ||||
87 | } | ||||
88 | | ||||
89 | void PolkitQt1Backend::initPolkitAgent(const QString &action, QWidget *parent /*= nullptr*/) const | ||||
90 | { | ||||
91 | if (!parent) { | ||||
92 | qWarning() << "Parent widget does not exists, can not proceed further"; | ||||
93 | return; | ||||
94 | } | ||||
95 | | ||||
96 | // Check if we are running terminal session or GUI session | ||||
97 | if (!qApp) { | ||||
98 | qWarning() << "We are running a TTY (Terminal) session"; | ||||
99 | qDebug() << "Can not proceed further since we do not support Text based Polkit Authentication Agent"; | ||||
100 | return; | ||||
101 | } | ||||
102 | | ||||
103 | // Get the dialog parent window Id | ||||
104 | quint64 parentWindowID = parent->effectiveWinId(); | ||||
105 | | ||||
106 | // Make a call to the KDE polkit Authentication Agent asking for it's services | ||||
107 | QDBusMessage callAgent = QDBusMessage::createMethodCall(QStringLiteral("org.kde.polkit-kde-authentication-agent-1"), QStringLiteral("/org/kde/Polkit1AuthAgent"), QStringLiteral("org.kde.Polkit1AuthAgent"), | ||||
108 | QStringLiteral("setWIdForAction")); | ||||
109 | | ||||
110 | callAgent << action; | ||||
111 | callAgent << parentWindowID; | ||||
112 | | ||||
113 | QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(callAgent); | ||||
114 | | ||||
115 | auto watcher = new QDBusPendingCallWatcher(call); | ||||
116 | | ||||
117 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, action, watcher](){ | ||||
118 | watcher->deleteLater(); | ||||
119 | const QDBusMessage reply = watcher->reply(); | ||||
120 | | ||||
121 | if (reply.type() == QDBusMessage::ErrorMessage) { | ||||
122 | qWarning() << "Could not call the Authentication Agent, Error:" << reply.errorMessage(); | ||||
123 | } | ||||
124 | }); | ||||
125 | } | ||||
126 | | ||||
127 | Authority::Result PolkitQt1Backend::actionStatus(const QString &action, const QByteArray &callerID) const | ||||
128 | { | ||||
129 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
130 | | ||||
131 | auto authority = Authority::instance(); | ||||
132 | | ||||
133 | PolkitEventLoop::m_result = authority->checkAuthorizationSync(action, subject, Authority::None); | ||||
134 | | ||||
135 | if (authority->hasError()) { | ||||
136 | qDebug() << "Encountered error while checking action status, Error code:" << authority->lastError() << "\n"; | ||||
137 | qDebug() << "Error Details:" << authority->errorDetails(); | ||||
138 | authority->clearError(); | ||||
139 | } | ||||
140 | | ||||
141 | return PolkitEventLoop::m_result; | ||||
142 | } | ||||
143 | | ||||
144 | QByteArray PolkitQt1Backend::callerID() const | ||||
145 | { | ||||
146 | return QDBusConnection::systemBus().baseService().toUtf8(); | ||||
147 | } | ||||
148 | | ||||
149 | bool PolkitQt1Backend::authorizeAction(const QString &action, const QByteArray &callerID) | ||||
150 | { | ||||
151 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
152 | | ||||
153 | auto authority = Authority::instance(); | ||||
154 | | ||||
155 | PolkitEventLoop event; | ||||
156 | event.processEvents(); | ||||
157 | | ||||
158 | connect(authority, &Authority::checkAuthorizationFinished, &event, &PolkitEventLoop::requestQuit); | ||||
159 | authority->checkAuthorizationSync(action, subject, Authority::AllowUserInteraction); | ||||
160 | | ||||
161 | event.exec(); | ||||
162 | | ||||
163 | if (authority->hasError()) { | ||||
164 | qWarning() << "Encountered error while checking authorization, Error code:" << authority->lastError() << "\n"; | ||||
165 | qDebug() << "Error details:" << authority->errorDetails(); | ||||
166 | | ||||
167 | // Clear all the errors from the buffer so that hasError() does not give previous error as a result when called later | ||||
168 | authority->clearError(); | ||||
169 | } | ||||
170 | | ||||
171 | if (event.result() /*PolkitEventLoop::m_result*/ == Authority::Yes) { | ||||
172 | return true; | ||||
173 | } else { | ||||
174 | return false; | ||||
175 | } | ||||
176 | } | ||||
177 | | ||||
178 | bool PolkitQt1Backend::revokeAuthorization(const QString &action, const QByteArray &callerID) | ||||
179 | { | ||||
180 | Q_UNUSED(action) | ||||
181 | | ||||
182 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
183 | | ||||
184 | auto authority = Authority::instance(); | ||||
185 | | ||||
186 | return authority->revokeTemporaryAuthorizationsSync(subject); | ||||
187 | } | ||||
188 | | ||||
189 | void PolkitQt1Backend::authStatusChanged() | ||||
190 | { | ||||
191 | for (auto it = m_cachedResults.begin(); it != m_cachedResults.end(); ++it) { | ||||
192 | const QString action = it.key(); | ||||
193 | QByteArray pid = QDBusConnection::systemBus().baseService().toUtf8(); | ||||
194 | if (it.value() != actionStatus(action, pid)) { | ||||
195 | *it = actionStatus(action, pid); | ||||
196 | } | ||||
197 | } | ||||
198 | | ||||
199 | // Force updating known actions | ||||
200 | Authority::instance()->enumerateActions(); | ||||
201 | m_flyingActions = true; | ||||
202 | } | ||||
203 | | ||||
204 | } // namespace Auth |