File: udisksstorageaccess.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (384 lines) | stat: -rw-r--r-- 12,002 bytes parent folder | download
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
    Copyright 2009 Pino Toscano <pino@kde.org>
    Copyright 2009-2012 Lukáš Tinkl <ltinkl@redhat.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "udisksstorageaccess.h"
#include "udisks2.h"

#include <QApplication>
#include <QDomDocument>
#include <QWidget>
#include <QtDBus>

using namespace Solid::Backends::UDisks2;

StorageAccess::StorageAccess(Device* device)
	: DeviceInterface(device), m_setupInProgress(false), m_teardownInProgress(false), m_passphraseRequested(false)
{
	connect(device, SIGNAL(changed()), this, SLOT(checkAccessibility()));
	updateCache();

	// Delay connecting to DBus signals to avoid the related time penalty
	// in hot paths such as predicate matching
	QTimer::singleShot(0, this, SLOT(connectDBusSignals()));
}

StorageAccess::~StorageAccess()
{
}

void StorageAccess::connectDBusSignals()
{
	m_device->registerAction("setup", this,
	                         SLOT(slotSetupRequested()),
	                         SLOT(slotSetupDone(int, const QString&)));

	m_device->registerAction("teardown", this,
	                         SLOT(slotTeardownRequested()),
	                         SLOT(slotTeardownDone(int, const QString&)));
}

bool StorageAccess::isLuksDevice() const
{
	return m_device->isEncryptedContainer();// encrypted device
}

bool StorageAccess::isAccessible() const
{
	if (isLuksDevice()) {// check if the cleartext slave is mounted
		const QString path = clearTextPath();
		//qDebug() << Q_FUNC_INFO << "CLEARTEXT device path: " << path;
		if (path.isEmpty() || path == "/")
			return false;
		Device holderDevice(path);
		return holderDevice.isMounted();
	}

	return m_device->isMounted();
}

static QString getMountPoint(const QVariant& mountPoints)
{
	QByteArrayList mntPoints = qdbus_cast<QByteArrayList>(mountPoints);
	if (!mntPoints.isEmpty()) {
		QByteArray mntPoint = mntPoints.first();
		if (mntPoint.size() > 0 && mntPoint.back() == '\0') {
			mntPoint.chop(1);
		}
		return QFile::decodeName(mntPoint);// FIXME Solid doesn't support multiple mount points
	}
	else
		return QString();
}

QString StorageAccess::filePath() const
{
	if (isLuksDevice()) {// encrypted (and unlocked) device
		const QString path = clearTextPath();
		if (path.isEmpty() || path == "/")
			return QString();
		Device holderDevice(path);
		return getMountPoint(holderDevice.prop("MountPoints"));
	}

	return getMountPoint(m_device->prop("MountPoints"));
}

bool StorageAccess::isIgnored() const
{
	return m_device->prop("HintIgnore").toBool();
}

bool StorageAccess::setup()
{
	if (m_teardownInProgress || m_setupInProgress)
		return false;
	m_setupInProgress = true;
	m_device->broadcastActionRequested("setup");

	if (m_device->isEncryptedContainer() && clearTextPath().isEmpty())
		return requestPassphrase();
	else
		return mount();
}

bool StorageAccess::teardown()
{
	if (m_teardownInProgress || m_setupInProgress)
		return false;
	m_teardownInProgress = true;
	m_device->broadcastActionRequested("teardown");

	return unmount();
}

void StorageAccess::updateCache()
{
	m_isAccessible = isAccessible();
}

void StorageAccess::checkAccessibility()
{
	const bool old_isAccessible = m_isAccessible;
	updateCache();

	if (old_isAccessible != m_isAccessible) {
		Q_EMIT accessibilityChanged(m_isAccessible, m_device->udi());
	}
}

void StorageAccess::slotDBusReply(const QDBusMessage& /*reply*/)
{
	const QString ctPath = clearTextPath();
	if (m_setupInProgress) {
		if (isLuksDevice() && !isAccessible()) {// unlocked device, now mount it
			mount();
		}
		else// Don't broadcast setupDone unless the setup is really done. (Fix kde#271156)
		{
			m_setupInProgress = false;
			m_device->broadcastActionDone("setup");

			checkAccessibility();
		}
	}
	else if (m_teardownInProgress)// FIXME
	{
		if (isLuksDevice() && !ctPath.isEmpty() && ctPath != "/")// unlocked device, lock it
		{
			callCryptoTeardown();
		}
		else if (!ctPath.isEmpty() && ctPath != "/") {
			callCryptoTeardown(true);// Lock crypted parent
		}
		else {
			// try to "eject" (aka safely remove) from the (parent) drive, e.g. SD card from a reader
			QString drivePath = m_device->drivePath();
			if (!drivePath.isEmpty() || drivePath != "/") {
				Device drive(drivePath);
				if (drive.prop("Ejectable").toBool() && drive.prop("MediaAvailable").toBool() && !m_device->isOpticalDisc())// optical drives have their Eject method
				{
					QDBusConnection c = QDBusConnection::systemBus();
					QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, drivePath, UD2_DBUS_INTERFACE_DRIVE, "Eject");
					msg << QVariantMap();// options, unused now
					c.call(msg, QDBus::NoBlock);
				}
			}

			m_teardownInProgress = false;
			m_device->broadcastActionDone("teardown");

			checkAccessibility();
		}
	}
}

void StorageAccess::slotDBusError(const QDBusError& error)
{
	//qDebug() << Q_FUNC_INFO << "DBUS ERROR:" << error.name() << error.message();

	if (m_setupInProgress) {
		m_setupInProgress = false;
		m_device->broadcastActionDone("setup", m_device->errorToSolidError(error.name()),
		                              m_device->errorToString(error.name()) + ": " + error.message());

		checkAccessibility();
	}
	else if (m_teardownInProgress) {
		m_teardownInProgress = false;
		m_device->broadcastActionDone("teardown", m_device->errorToSolidError(error.name()),
		                              m_device->errorToString(error.name()) + ": " + error.message());
		checkAccessibility();
	}
}

void StorageAccess::slotSetupRequested()
{
	m_setupInProgress = true;
	//qDebug() << "SETUP REQUESTED:" << m_device->udi();
	Q_EMIT setupRequested(m_device->udi());
}

void StorageAccess::slotSetupDone(int error, const QString& errorString)
{
	m_setupInProgress = false;
	//qDebug() << "SETUP DONE:" << m_device->udi();
	Q_EMIT setupDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());

	checkAccessibility();
}

void StorageAccess::slotTeardownRequested()
{
	m_teardownInProgress = true;
	Q_EMIT teardownRequested(m_device->udi());
}

void StorageAccess::slotTeardownDone(int error, const QString& errorString)
{
	m_teardownInProgress = false;
	Q_EMIT teardownDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());

	checkAccessibility();
}

bool StorageAccess::mount()
{
	QString path = m_device->udi();
	const QString ctPath = clearTextPath();

	if (isLuksDevice() && !ctPath.isEmpty()) {// mount options for the cleartext volume
		path = ctPath;
	}

	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, path, UD2_DBUS_INTERFACE_FILESYSTEM, "Mount");
	QVariantMap options;

	if (m_device->prop("IdType").toString() == "vfat")
		options.insert("options", "flush");

	msg << options;

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(const QDBusMessage&)),
	                          SLOT(slotDBusError(const QDBusError&)));
}

bool StorageAccess::unmount()
{
	QString path = m_device->udi();
	const QString ctPath = clearTextPath();

	if (isLuksDevice() && !ctPath.isEmpty()) {// unmount options for the cleartext volume
		path = ctPath;
	}

	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, path, UD2_DBUS_INTERFACE_FILESYSTEM, "Unmount");

	msg << QVariantMap();// options, unused now

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(const QDBusMessage&)),
	                          SLOT(slotDBusError(const QDBusError&)),
	                          s_unmountTimeout);
}

QString StorageAccess::generateReturnObjectPath()
{
	static int number = 1;

	return "/org/kde/solid/UDisks2StorageAccess_" + QString::number(number++);
}

QString StorageAccess::clearTextPath() const
{
	const QString prefix = "/org/freedesktop/UDisks2/block_devices";
	QDBusMessage call = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, prefix,
	                                                   DBUS_INTERFACE_INTROSPECT, "Introspect");
	QDBusPendingReply<QString> reply = QDBusConnection::systemBus().asyncCall(call);
	reply.waitForFinished();

	if (reply.isValid()) {
		QDomDocument dom;
		dom.setContent(reply.value());
		QDomNodeList nodeList = dom.documentElement().elementsByTagName("node");
		for (int i = 0; i < nodeList.count(); i++) {
			QDomElement nodeElem = nodeList.item(i).toElement();
			if (!nodeElem.isNull() && nodeElem.hasAttribute("name")) {
				const QString udi = prefix + "/" + nodeElem.attribute("name");
				Device holderDevice(udi);

				if (m_device->udi() == holderDevice.prop("CryptoBackingDevice").value<QDBusObjectPath>().path()) {
					//qDebug() << Q_FUNC_INFO << "CLEARTEXT device path: " << udi;
					return udi;
				}
			}
		}
	}

	return QString();
}

bool StorageAccess::requestPassphrase()
{
	QString udi = m_device->udi();
	QString returnService = QDBusConnection::sessionBus().baseService();
	m_lastReturnObject = generateReturnObjectPath();

	QDBusConnection::sessionBus().registerObject(m_lastReturnObject, this, QDBusConnection::ExportScriptableSlots);

	QWidget* activeWindow = QApplication::activeWindow();
	uint wId = 0;
	if (activeWindow != nullptr)
		wId = (uint)activeWindow->winId();

	QString appId = QCoreApplication::applicationName();

	QDBusInterface soliduiserver("org.kde.kded", "/modules/soliduiserver", "org.kde.SolidUiServer");
	QDBusReply<void> reply = soliduiserver.call("showPassphraseDialog", udi, returnService,
	                                            m_lastReturnObject, wId, appId);
	m_passphraseRequested = reply.isValid();
	if (!m_passphraseRequested)
		qWarning() << "Failed to call the SolidUiServer, D-Bus said:" << reply.error();

	return m_passphraseRequested;
}

void StorageAccess::passphraseReply(const QString& passphrase)
{
	if (m_passphraseRequested) {
		QDBusConnection::sessionBus().unregisterObject(m_lastReturnObject);
		m_passphraseRequested = false;
		if (!passphrase.isEmpty())
			callCryptoSetup(passphrase);
		else {
			m_setupInProgress = false;
			m_device->broadcastActionDone("setup");
		}
	}
}

void StorageAccess::callCryptoSetup(const QString& passphrase)
{
	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, m_device->udi(), UD2_DBUS_INTERFACE_ENCRYPTED, "Unlock");

	msg << passphrase;
	msg << QVariantMap();// options, unused now

	c.callWithCallback(msg, this,
	                   SLOT(slotDBusReply(const QDBusMessage&)),
	                   SLOT(slotDBusError(const QDBusError&)));
}

bool StorageAccess::callCryptoTeardown(bool actOnParent)
{
	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE,
	                                                  actOnParent ? (m_device->prop("CryptoBackingDevice").value<QDBusObjectPath>().path()) : m_device->udi(),
	                                                  UD2_DBUS_INTERFACE_ENCRYPTED, "Lock");
	msg << QVariantMap();// options, unused now

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(const QDBusMessage&)),
	                          SLOT(slotDBusError(const QDBusError&)));
}