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 <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 | Authority::Result PolkitEventLoop::result() const | ||||
50 | { | ||||
51 | return m_result; | ||||
52 | } | ||||
53 | | ||||
54 | PolkitQt1Backend::PolkitQt1Backend() | ||||
55 | : m_flyingActions(false) | ||||
56 | { | ||||
57 | // Connect various useful Polkit signals | ||||
58 | connect(Authority::instance(), &Authority::configChanged, this, &Auth::PolkitQt1Backend::authStatusChanged); | ||||
59 | connect(Authority::instance(), &Authority::consoleKitDBChanged, this, &Auth::PolkitQt1Backend::authStatusChanged); | ||||
60 | | ||||
61 | m_flyingActions = true; | ||||
62 | } | ||||
63 | | ||||
64 | PolkitQt1Backend::~PolkitQt1Backend() | ||||
65 | { | ||||
66 | | ||||
67 | } | ||||
68 | | ||||
69 | void PolkitQt1Backend::initPolkitAgent(const QString &action, QWidget *parent /*= nullptr*/) const | ||||
70 | { | ||||
71 | if (!parent) { | ||||
72 | qWarning() << "Parent widget does not exists, can not proceed further"; | ||||
73 | return; | ||||
74 | } | ||||
75 | | ||||
76 | // Check if we are running terminal session or GUI session | ||||
77 | if (!qApp) { | ||||
78 | qWarning() << "We are running a TTY (Terminal) session"; | ||||
79 | qDebug() << "Can not proceed further since we do not support Text based Polkit Authentication Agent"; | ||||
80 | return; | ||||
81 | } | ||||
82 | | ||||
83 | // Get the dialog parent window Id | ||||
84 | quint64 parentWindowID = parent->effectiveWinId(); | ||||
85 | | ||||
86 | // Make a call to the KDE polkit Authentication Agent asking for it's services | ||||
87 | QDBusMessage callAgent = QDBusMessage::createMethodCall(QLatin1String("org.kde.polkit-kde-authentication-agent-1"), QLatin1String("/org/kde/Polkit1AuthAgent"), QLatin1String("org.kde.Polkit1AuthAgent"), | ||||
88 | QLatin1String("setWindowIdForAction")); | ||||
89 | | ||||
90 | callAgent << action; | ||||
91 | callAgent << parentWindowID; | ||||
92 | | ||||
93 | QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(callAgent); | ||||
94 | call.waitForFinished(); | ||||
95 | | ||||
96 | auto watcher = new QDBusPendingCallWatcher(call); | ||||
97 | | ||||
98 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, action, watcher](){ | ||||
99 | | ||||
100 | const QDBusMessage reply = watcher->reply(); | ||||
101 | | ||||
102 | if (reply.type() == QDBusMessage::ErrorMessage) { | ||||
103 | qWarning() << "Could not call the Authentication Agent, Error:" << reply.errorMessage(); | ||||
104 | } | ||||
105 | | ||||
106 | watcher->deleteLater(); | ||||
107 | }); | ||||
108 | } | ||||
109 | | ||||
110 | Authority::Result PolkitQt1Backend::actionStatus(const QString &action, const QByteArray &callerID) const | ||||
111 | { | ||||
112 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
113 | | ||||
114 | auto authority = Authority::instance(); | ||||
115 | | ||||
116 | auto result = authority->checkAuthorizationSync(action, subject, Authority::AllowUserInteraction); | ||||
117 | | ||||
118 | if (authority->hasError()) { | ||||
119 | qDebug() << "Encountered error while checking action status, Error code:" << authority->lastError() << "\n"; | ||||
120 | qDebug() << "Error Details:" << authority->errorDetails(); | ||||
121 | authority->clearError(); | ||||
122 | } | ||||
123 | | ||||
124 | return result; | ||||
125 | } | ||||
126 | | ||||
127 | QByteArray PolkitQt1Backend::callerID() const | ||||
128 | { | ||||
129 | return QDBusConnection::systemBus().baseService().toUtf8(); | ||||
130 | } | ||||
131 | | ||||
132 | bool PolkitQt1Backend::authorizeAction(const QString &action, const QByteArray &callerID) | ||||
133 | { | ||||
134 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
135 | | ||||
136 | auto authority = Authority::instance(); | ||||
137 | | ||||
138 | PolkitEventLoop event; | ||||
139 | event.processEvents(); | ||||
140 | | ||||
141 | connect(authority, &Authority::checkAuthorizationFinished, &event, &PolkitEventLoop::quit); | ||||
142 | | ||||
143 | authority->checkAuthorization(action, subject, Authority::AllowUserInteraction); | ||||
144 | | ||||
145 | event.exec(); | ||||
146 | | ||||
147 | if (authority->hasError()) { | ||||
148 | qWarning() << "Encountered error while checking authorization, Error code:" << authority->lastError() << "\n"; | ||||
149 | qDebug() << "Error details:" << authority->errorDetails(); | ||||
150 | | ||||
151 | // Clear all the errors from the buffer so that hasError() does not give previous error as a result when called later | ||||
152 | authority->clearError(); | ||||
153 | } | ||||
154 | | ||||
155 | if (event.result() == Authority::Yes) { | ||||
156 | return true; | ||||
157 | } else { | ||||
158 | return false; | ||||
159 | } | ||||
160 | } | ||||
161 | | ||||
162 | bool PolkitQt1Backend::revokeAuthorization(const QString &action, const QByteArray &callerID) | ||||
163 | { | ||||
164 | Q_UNUSED(action) | ||||
165 | | ||||
166 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
167 | | ||||
168 | auto authority = Authority::instance(); | ||||
169 | | ||||
170 | return authority->revokeTemporaryAuthorizationsSync(subject); | ||||
171 | } | ||||
172 | | ||||
173 | void PolkitQt1Backend::authStatusChanged() | ||||
174 | { | ||||
175 | for (auto it = m_cachedResults.begin(); it != m_cachedResults.end(); ++it) { | ||||
176 | const QString action = it.key(); | ||||
177 | QByteArray pid = QDBusConnection::systemBus().baseService().toUtf8(); | ||||
178 | if (it.value() != actionStatus(action, pid)) { | ||||
179 | *it = actionStatus(action, pid); | ||||
180 | } | ||||
181 | } | ||||
182 | | ||||
183 | // Force updating known actions | ||||
184 | Authority::instance()->enumerateActions(); | ||||
185 | m_flyingActions = true; | ||||
186 | } | ||||
187 | | ||||
188 | } // namespace Auth |