Changeset View
Changeset View
Standalone View
Standalone View
ksmserver/policykit-agent/policykitlistener.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.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 | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library 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 GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QDBusConnection> | ||||
22 | #include <QDebug> | ||||
23 | | ||||
24 | #include <KWindowSystem> | ||||
25 | | ||||
26 | #include <PolkitQt1/Agent/Listener> | ||||
27 | #include <PolkitQt1/Agent/Session> | ||||
28 | #include <PolkitQt1/Subject> | ||||
29 | #include <PolkitQt1/Identity> | ||||
30 | #include <PolkitQt1/Details> | ||||
31 | | ||||
32 | #include "policykitlistener.h" | ||||
33 | #include "AuthDialog.h" | ||||
34 | #include "polkit1authagentadaptor.h" | ||||
35 | | ||||
36 | PolicyKitListener::PolicyKitListener(QObject *parent) | ||||
37 | : Listener(parent) | ||||
38 | , m_inProgress(false) | ||||
39 | , m_selectedUser(0) | ||||
40 | { | ||||
41 | (void) new Polkit1AuthAgentAdaptor(this); | ||||
42 | | ||||
43 | if (!QDBusConnection::sessionBus().registerObject("/org/kde/Polkit1AuthAgent", this, | ||||
44 | QDBusConnection::ExportScriptableSlots | | ||||
45 | QDBusConnection::ExportScriptableProperties | | ||||
46 | QDBusConnection::ExportAdaptors)) { | ||||
47 | qWarning() << "Could not initiate DBus helper!"; | ||||
48 | } | ||||
49 | | ||||
50 | qDebug() << "Listener online"; | ||||
51 | } | ||||
52 | | ||||
53 | PolicyKitListener::~PolicyKitListener() | ||||
54 | { | ||||
55 | } | ||||
56 | | ||||
57 | void PolicyKitListener::setWIdForAction(const QString& action, qulonglong wID) | ||||
58 | { | ||||
59 | qDebug() << "On to the handshake"; | ||||
60 | m_actionsToWID[action] = wID; | ||||
61 | } | ||||
62 | | ||||
63 | void PolicyKitListener::initiateAuthentication(const QString &actionId, | ||||
64 | const QString &message, | ||||
65 | const QString &iconName, | ||||
66 | const PolkitQt1::Details &details, | ||||
67 | const QString &cookie, | ||||
68 | const PolkitQt1::Identity::List &identities, | ||||
69 | PolkitQt1::Agent::AsyncResult* result) | ||||
70 | { | ||||
71 | qDebug() << "Initiating authentication"; | ||||
72 | | ||||
73 | if (m_inProgress) { | ||||
74 | result->setError(i18n("Another client is already authenticating, please try again later.")); | ||||
75 | result->setCompleted(); | ||||
76 | qDebug() << "Another client is already authenticating, please try again later."; | ||||
77 | return; | ||||
78 | } | ||||
79 | | ||||
80 | m_identities = identities; | ||||
81 | m_cookie = cookie; | ||||
82 | m_result = result; | ||||
83 | m_session.clear(); | ||||
84 | | ||||
85 | m_inProgress = true; | ||||
86 | | ||||
87 | WId parentId = 0; | ||||
88 | | ||||
89 | if (m_actionsToWID.contains(actionId)) { | ||||
90 | parentId = m_actionsToWID[actionId]; | ||||
91 | } | ||||
92 | | ||||
93 | m_dialog = new AuthDialog(actionId, message, iconName, details, identities, parentId); | ||||
94 | connect(m_dialog.data(), SIGNAL(okClicked()), SLOT(dialogAccepted())); | ||||
95 | connect(m_dialog.data(), SIGNAL(rejected()), SLOT(dialogCanceled())); | ||||
96 | connect(m_dialog.data(), SIGNAL(adminUserSelected(PolkitQt1::Identity)), SLOT(userSelected(PolkitQt1::Identity))); | ||||
97 | | ||||
98 | qDebug() << "WinId of the dialog is " << m_dialog.data()->winId() << m_dialog.data()->effectiveWinId(); | ||||
99 | m_dialog.data()->setOptions(); | ||||
100 | m_dialog.data()->show(); | ||||
101 | KWindowSystem::forceActiveWindow(m_dialog.data()->winId()); | ||||
102 | qDebug() << "WinId of the shown dialog is " << m_dialog.data()->winId() << m_dialog.data()->effectiveWinId(); | ||||
103 | | ||||
104 | if (identities.length() == 1) { | ||||
105 | m_selectedUser = identities[0]; | ||||
106 | } else { | ||||
107 | m_selectedUser = m_dialog.data()->adminUserSelected(); | ||||
108 | } | ||||
109 | | ||||
110 | m_numTries = 0; | ||||
111 | tryAgain(); | ||||
112 | } | ||||
113 | | ||||
114 | void PolicyKitListener::tryAgain() | ||||
115 | { | ||||
116 | qDebug() << "Trying again"; | ||||
117 | m_wasCancelled = false; | ||||
118 | | ||||
119 | // We will create new session only when some user is selected | ||||
120 | if (m_selectedUser.isValid()) { | ||||
121 | m_session = new Session(m_selectedUser, m_cookie, m_result); | ||||
122 | connect(m_session.data(), SIGNAL(request(QString,bool)), this, SLOT(request(QString,bool))); | ||||
123 | connect(m_session.data(), SIGNAL(completed(bool)), this, SLOT(completed(bool))); | ||||
124 | connect(m_session.data(), SIGNAL(showError(QString)), this, SLOT(showError(QString))); | ||||
125 | | ||||
126 | m_session.data()->initiate(); | ||||
127 | } | ||||
128 | | ||||
129 | } | ||||
130 | | ||||
131 | void PolicyKitListener::finishObtainPrivilege() | ||||
132 | { | ||||
133 | qDebug() << "Finishing obtaining privileges"; | ||||
134 | | ||||
135 | // Number of tries increase only when some user is selected | ||||
136 | if (m_selectedUser.isValid()) { | ||||
137 | m_numTries++; | ||||
138 | } | ||||
139 | | ||||
140 | if (!m_gainedAuthorization && !m_wasCancelled && !m_dialog.isNull()) { | ||||
141 | m_dialog.data()->authenticationFailure(); | ||||
142 | | ||||
143 | if (m_numTries < 3) { | ||||
144 | m_session.data()->deleteLater(); | ||||
145 | | ||||
146 | tryAgain(); | ||||
147 | return; | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | if (!m_session.isNull()) { | ||||
152 | m_session.data()->result()->setCompleted(); | ||||
153 | } else { | ||||
154 | m_result->setCompleted(); | ||||
155 | } | ||||
156 | m_session.data()->deleteLater(); | ||||
157 | | ||||
158 | if (!m_dialog.isNull()) { | ||||
159 | m_dialog.data()->hide(); | ||||
160 | m_dialog.data()->deleteLater(); | ||||
161 | } | ||||
162 | | ||||
163 | m_inProgress = false; | ||||
164 | | ||||
165 | qDebug() << "Finish obtain authorization:" << m_gainedAuthorization; | ||||
166 | } | ||||
167 | | ||||
168 | bool PolicyKitListener::initiateAuthenticationFinish() | ||||
169 | { | ||||
170 | qDebug() << "Finishing authentication"; | ||||
171 | return true; | ||||
172 | } | ||||
173 | | ||||
174 | void PolicyKitListener::cancelAuthentication() | ||||
175 | { | ||||
176 | qDebug() << "Cancelling authentication"; | ||||
177 | | ||||
178 | m_wasCancelled = true; | ||||
179 | finishObtainPrivilege(); | ||||
180 | } | ||||
181 | | ||||
182 | void PolicyKitListener::request(const QString &request, bool echo) | ||||
183 | { | ||||
184 | Q_UNUSED(echo); | ||||
185 | qDebug() << "Request: " << request; | ||||
186 | | ||||
187 | if (!m_dialog.isNull()) { | ||||
188 | m_dialog.data()->setRequest(request, m_selectedUser.isValid() && | ||||
189 | m_selectedUser.toString() == "unix-user:root"); | ||||
190 | } | ||||
191 | } | ||||
192 | | ||||
193 | void PolicyKitListener::completed(bool gainedAuthorization) | ||||
194 | { | ||||
195 | qDebug() << "Completed: " << gainedAuthorization; | ||||
196 | | ||||
197 | m_gainedAuthorization = gainedAuthorization; | ||||
198 | | ||||
199 | finishObtainPrivilege(); | ||||
200 | } | ||||
201 | | ||||
202 | void PolicyKitListener::showError(const QString &text) | ||||
203 | { | ||||
204 | qDebug() << "Error: " << text; | ||||
205 | } | ||||
206 | | ||||
207 | void PolicyKitListener::dialogAccepted() | ||||
208 | { | ||||
209 | qDebug() << "Dialog accepted"; | ||||
210 | | ||||
211 | if (!m_dialog.isNull()) { | ||||
212 | m_session.data()->setResponse(m_dialog.data()->password()); | ||||
213 | } | ||||
214 | } | ||||
215 | | ||||
216 | void PolicyKitListener::dialogCanceled() | ||||
217 | { | ||||
218 | qDebug() << "Dialog cancelled"; | ||||
219 | | ||||
220 | m_wasCancelled = true; | ||||
221 | if (!m_session.isNull()) { | ||||
222 | m_session.data()->cancel(); | ||||
223 | } | ||||
224 | | ||||
225 | finishObtainPrivilege(); | ||||
226 | } | ||||
227 | | ||||
228 | void PolicyKitListener::userSelected(const PolkitQt1::Identity &identity) | ||||
229 | { | ||||
230 | m_selectedUser = identity; | ||||
231 | // If some user is selected we must destroy existing session | ||||
232 | if (!m_session.isNull()) { | ||||
233 | m_session.data()->deleteLater(); | ||||
234 | } | ||||
235 | tryAgain(); | ||||
236 | } |