1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* This file is part of the KDE project
Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "polkit-listener.h"
#include "AuthDialog.h"
#include <kdebug.h>
#include <tdelocale.h>
#include "twin.h"
#include <PolkitTQt/Agent/Listener>
#include <PolkitTQt/Agent/Session>
#include <PolkitTQt/Subject>
#include <PolkitTQt/Identity>
#include <PolkitTQt/Details>
using namespace PolkitTQt;
using namespace PolkitTQt::Agent;
PolkitListener::PolkitListener(TQObject *parent) : Listener(parent), m_inProgress(false),
m_selectedUser(0), m_result(nullptr), m_session(nullptr)
{
PolkitTQt::UnixSessionSubject session(getpid());
if (!registerListener(session, "/org/trinitydesktop/PolKit1AuthAgent"))
{
kdWarning() << "Could not initiate DBus listener!" << endl;
}
kdDebug() << "Listener online" << endl;
}
void PolkitListener::clearSession()
{
if (m_session)
{
m_session->deleteLater();
m_session = nullptr;
}
}
void PolkitListener::initiateAuthentication(const TQString &actionId, const TQString &message,
const TQString &iconName, const PolkitTQt::Details &details,
const TQString &cookie, const Identity::List &identities, AsyncResult* result)
{
kdDebug() << "Initiating authentication" << endl;
if (m_inProgress)
{
result->setError(i18n("Another client is already authenticating, please try again later."));
result->setCompleted();
kdDebug() << "Another client is already authenticating, please try again later." << endl;
return;
}
m_identities = identities;
m_cookie = cookie;
m_result = result;
clearSession();
m_inProgress = true;
m_dialog = new AuthDialog(actionId, message, iconName, details, identities);
connect(m_dialog, TQT_SIGNAL(okClicked()), TQT_SLOT(dialogAccepted()));
connect(m_dialog, TQT_SIGNAL(cancelClicked()), TQT_SLOT(dialogCanceled()));
connect(m_dialog, TQT_SIGNAL(adminUserSelected(const PolkitTQt::Identity&)),
TQT_SLOT(userSelected(const PolkitTQt::Identity&)));
m_dialog->setOptions();
m_dialog->show();
KWin::forceActiveWindow(m_dialog->winId());
if (identities.count() == 1)
{
m_selectedUser = identities[0];
}
else
{
m_selectedUser = m_dialog->adminUserSelected();
}
m_numTries = 0;
tryAgain();
}
void PolkitListener::tryAgain()
{
kdDebug() << "Trying again" << endl;
// test!!!
m_wasCancelled = false;
// We will create new session only when some user is selected
if (m_selectedUser.isValid())
{
m_session = new Session(m_selectedUser, m_cookie, m_result);
connect(m_session, TQT_SIGNAL(request(const TQString&, bool)), this,
TQT_SLOT(request(const TQString&, bool)));
connect(m_session, TQT_SIGNAL(completed(bool)), this, TQT_SLOT(completed(bool)));
connect(m_session, TQT_SIGNAL(showError(const TQString&)), this,
TQT_SLOT(showError(const TQString&)));
m_session->initiate();
}
}
void PolkitListener::finishObtainPrivilege()
{
kdDebug() << "Finishing obtaining privileges" << endl;
// Number of tries increase only when some user is selected
if (m_selectedUser.isValid())
{
m_numTries++;
}
if (!m_gainedAuthorization && !m_wasCancelled && m_dialog)
{
m_dialog->authenticationFailure();
if (m_numTries < 3)
{
clearSession();
tryAgain();
return;
}
}
if (m_session)
{
m_session->result()->setCompleted();
}
else
{
m_result->setCompleted();
}
clearSession();
if (m_dialog)
{
m_dialog->hide();
m_dialog->deleteLater();
m_dialog = nullptr;
}
m_inProgress = false;
kdDebug() << "Finish obtain authorization:" << m_gainedAuthorization << endl;
}
bool PolkitListener::initiateAuthenticationFinish()
{
kdDebug() << "Finishing authentication" << endl;
return true;
}
void PolkitListener::cancelAuthentication()
{
kdDebug() << "Cancelling authentication" << endl;
m_wasCancelled = true;
finishObtainPrivilege();
}
void PolkitListener::request(const TQString &request, bool _)
{
kdDebug() << "Request: " << request << endl;
if (m_dialog)
{
m_dialog->setRequest(request, m_selectedUser.isValid() &&
m_selectedUser.toString() == "unix-user:root");
}
}
void PolkitListener::completed(bool gainedAuthorization)
{
kdDebug() << "Completed: " << gainedAuthorization << endl;
m_gainedAuthorization = gainedAuthorization;
finishObtainPrivilege();
}
void PolkitListener::showError(const TQString &text)
{
kdDebug() << "Error: " << text << endl;
}
void PolkitListener::dialogAccepted()
{
kdDebug() << "Dialog accepted" << endl;
if (m_session)
{
m_session->setResponse(m_dialog->password());
}
}
void PolkitListener::dialogCanceled()
{
kdDebug() << "Dialog cancelled" << endl;
m_wasCancelled = true;
if (m_session)
{
m_session->cancel();
}
finishObtainPrivilege();
}
void PolkitListener::userSelected(const PolkitTQt::Identity &identity)
{
m_selectedUser = identity;
// If some user is selected we must destroy existing session
clearSession();
tryAgain();
}
#include "polkit-listener.moc"
|