Changeset View
Changeset View
Standalone View
Standalone View
src/util/externalcommand_polkitbackend.cpp
- This file was added.
1 | /************************************************************************* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by Shubham <shubham.jangra@kdemail.net> * | ||||
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 <QDebug> | ||||
22 | #include <QDBusConnection> | ||||
23 | #include <QDBusConnectionInterface> | ||||
24 | #include <QEventLoop> | ||||
25 | #include <QString> | ||||
26 | #include <QTimer> | ||||
27 | #include <QWidget> | ||||
28 | | ||||
29 | #include <PolkitQt1/Authority> | ||||
30 | #include <PolkitQt1/Subject> | ||||
31 | | ||||
32 | using namespace PolkitQt1; | ||||
33 | | ||||
34 | namespace Auth | ||||
35 | { | ||||
36 | | ||||
37 | PolkitEventLoop::PolkitEventLoop(QObject *parent) | ||||
38 | : QEventLoop(qobject_cast<QEventLoop *>(parent)), | ||||
39 | m_result(Authority::No) | ||||
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 | QTimer::singleShot(1000, this, PolkitQt1Backend::quit); | ||||
53 | quit(); | ||||
54 | } | ||||
55 | | ||||
56 | Authority::Result PolkitEventLoop::result() const | ||||
57 | { | ||||
58 | return m_result; | ||||
59 | } | ||||
60 | | ||||
61 | PolkitQt1Backend::PolkitQt1Backend() | ||||
62 | : m_flyingActions(false) | ||||
63 | { | ||||
64 | // Connect various useful Polkit signals | ||||
65 | connect(Authority::instance(), &Authority::configChanged, this, &Auth::PolkitQt1Backend::authStatusChanged); | ||||
66 | connect(Authority::instance(), &Authority::consoleKitDBChanged, this, &Auth::PolkitQt1Backend::authStatusChanged); | ||||
67 | | ||||
68 | m_flyingActions = true; | ||||
69 | } | ||||
70 | | ||||
71 | PolkitQt1Backend::~PolkitQt1Backend() | ||||
72 | { | ||||
73 | | ||||
74 | } | ||||
75 | | ||||
76 | void PolkitQt1Backend::initPolkitAgent(const QString &action, QWidget *parent /*= nullptr*/) const | ||||
77 | { | ||||
78 | if (!parent) { | ||||
79 | qWarning() << "Parent widget does not exists, can not proceed further"; | ||||
80 | return; | ||||
81 | } | ||||
82 | | ||||
83 | // Check if we are running terminal session or GUI session | ||||
84 | if (!qApp) { | ||||
85 | qWarning() << "We are running a TTY (Terminal) session"; | ||||
86 | qDebug() << "Can not proceed further since we do not support Text based Polkit Authentication Agent"; | ||||
87 | return; | ||||
88 | } | ||||
89 | | ||||
90 | // Get the dialog parent window Id | ||||
91 | quint64 parentWindowID = parent->effectiveWinId(); | ||||
92 | | ||||
93 | // Make a call to the KDE polkit Authentication Agent asking for it's services | ||||
94 | QDBusMessage callAgent = QDBusMessage::createMethodCall(QLatin1String("org.kde.polkit-kde-authentication-agent-1"), QLatin1String("/org/kde/Polkit1AuthAgent"), QLatin1String("org.kde.Polkit1AuthAgent"), | ||||
95 | QLatin1String("setWindowIdForAction")); | ||||
96 | | ||||
97 | callAgent << action; | ||||
98 | callAgent << parentWindowID; | ||||
99 | | ||||
100 | QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(callAgent); | ||||
101 | call.waitForFinished(); | ||||
102 | | ||||
103 | auto watcher = new QDBusPendingCallWatcher(call); | ||||
104 | | ||||
105 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, action, watcher](){ | ||||
106 | | ||||
107 | const QDBusMessage reply = watcher->reply(); | ||||
108 | | ||||
109 | if (reply.type() == QDBusMessage::ErrorMessage) { | ||||
110 | qWarning() << "Could not call the Authentication Agent, Error:" << reply.errorMessage(); | ||||
111 | } | ||||
112 | | ||||
113 | watcher->deleteLater(); | ||||
114 | }); | ||||
115 | } | ||||
116 | | ||||
117 | Authority::Result PolkitQt1Backend::actionStatus(const QString &action, const QByteArray &callerID) const | ||||
118 | { | ||||
119 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
120 | | ||||
121 | auto authority = Authority::instance(); | ||||
122 | | ||||
123 | auto result = authority->checkAuthorizationSync(action, subject, Authority::AllowUserInteraction); | ||||
124 | | ||||
125 | if (authority->hasError()) { | ||||
126 | qDebug() << "Encountered error while checking action status, Error code:" << authority->lastError() << "\n"; | ||||
127 | qDebug() << "Error Details:" << authority->errorDetails(); | ||||
128 | authority->clearError(); | ||||
129 | } | ||||
130 | | ||||
131 | return result; | ||||
132 | } | ||||
133 | | ||||
134 | QByteArray PolkitQt1Backend::callerID() const | ||||
135 | { | ||||
136 | return QDBusConnection::systemBus().baseService().toUtf8(); | ||||
137 | } | ||||
138 | | ||||
139 | bool PolkitQt1Backend::authorizeAction(const QString &action, const QByteArray &callerID) | ||||
140 | { | ||||
141 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
142 | | ||||
143 | auto authority = Authority::instance(); | ||||
144 | | ||||
145 | PolkitEventLoop event;//(qobject_cast<QObject *>(qApp)); | ||||
146 | | ||||
147 | connect(authority, &Authority::checkAuthorizationFinished, &event, &PolkitEventLoop::requestQuit); | ||||
148 | | ||||
149 | authority->checkAuthorizationSync(action, subject, Authority::AllowUserInteraction); | ||||
150 | | ||||
151 | event.exec(); | ||||
152 | | ||||
153 | if (authority->hasError()) { | ||||
154 | qWarning() << "Encountered error while checking authorization, Error code:" << authority->lastError() << "\n"; | ||||
155 | qDebug() << "Error details:" << authority->errorDetails(); | ||||
156 | | ||||
157 | // Clear all the errors from the buffer so that hasError() does not give previous error as a result when called later | ||||
158 | authority->clearError(); | ||||
159 | } | ||||
160 | | ||||
161 | if (event.result() == Authority::Yes) { | ||||
162 | // Emit signal signalling that current action is authorized by the authority instance | ||||
163 | emit actionAuthorized(true); | ||||
164 | return true; | ||||
165 | } else { | ||||
166 | emit actionAuthorized(false); | ||||
167 | return false; | ||||
168 | } | ||||
169 | } | ||||
170 | | ||||
171 | bool stopAction(const QString &action, const QByteArray &callerID) | ||||
172 | { | ||||
173 | Q_UNUSED(action) | ||||
174 | | ||||
175 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
176 | | ||||
177 | auto authority = Authority::instance(); | ||||
178 | | ||||
179 | PolkitEventLoop event;//(qobject_cast<QObject *>(qApp)); | ||||
180 | event.exec(); | ||||
181 | | ||||
182 | return authority->revokeTemporaryAuthorizationsSync(subject); | ||||
183 | } | ||||
184 | | ||||
185 | // Dummy function for QTimer | ||||
186 | void PolkitQt1Backend::quit() | ||||
187 | { | ||||
188 | | ||||
189 | } | ||||
190 | | ||||
191 | void PolkitQt1Backend::authStatusChanged() | ||||
192 | { | ||||
193 | for (auto it = m_cachedResults.begin(); it != m_cachedResults.end(); ++it) { | ||||
194 | const QString action = it.key(); | ||||
195 | QByteArray pid = QDBusConnection::systemBus().baseService().toUtf8(); | ||||
196 | if (it.value() != actionStatus(action, pid)) { | ||||
197 | *it = actionStatus(action, pid); | ||||
198 | } | ||||
199 | } | ||||
200 | | ||||
201 | // Force updating known actions | ||||
202 | Authority::instance()->enumerateActions(); | ||||
203 | m_flyingActions = true; | ||||
204 | } | ||||
205 | | ||||
206 | } // namespace Auth | ||||
207 | | ||||
208 | #include "moc_externalcommand_polkitbackend.cpp" |