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 (378 lines) | stat: -rw-r--r-- 11,959 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
/*
    Copyright 2009 Pino Toscano <pino@kde.org>
    Copyright 2009, 2011 Lukas 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 "udisks.h"

#include <QApplication>
#include <QProcess>
#include <QWidget>
#include <QtDBus>

using namespace Solid::Backends::UDisks;

UDisksStorageAccess::UDisksStorageAccess(UDisksDevice* device)
	: DeviceInterface(device), m_setupInProgress(false), m_teardownInProgress(false), m_passphraseRequested(false)
{
	connect(device, SIGNAL(changed()), this, SLOT(slotChanged()));
	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()));
}

UDisksStorageAccess::~UDisksStorageAccess()
{
}

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

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

bool UDisksStorageAccess::isLuksDevice() const
{
	return m_device->prop("DeviceIsLuks").toBool();
}

bool UDisksStorageAccess::isAccessible() const
{
	if (isLuksDevice()) {// check if the cleartext slave is mounted
		UDisksDevice holderDevice(m_device->prop("LuksHolder").value<QDBusObjectPath>().path());
		return holderDevice.prop("DeviceIsMounted").toBool();
	}

	return m_device->prop("DeviceIsMounted").toBool();
}

QString UDisksStorageAccess::filePath() const
{
	if (!isAccessible())
		return QString();

	QStringList mntPoints;

	if (isLuksDevice()) {// encrypted (and unlocked) device
		QString path = m_device->prop("LuksHolder").value<QDBusObjectPath>().path();
		if (path.isEmpty() || path == "/")
			return QString();
		UDisksDevice holderDevice(path);
		mntPoints = holderDevice.prop("DeviceMountPaths").toStringList();
		if (!mntPoints.isEmpty())
			return mntPoints.first();// FIXME Solid doesn't support multiple mount points
		else
			return QString();
	}

	mntPoints = m_device->prop("DeviceMountPaths").toStringList();

	if (!mntPoints.isEmpty())
		return mntPoints.first();// FIXME Solid doesn't support multiple mount points
	else
		return QString();
}

bool UDisksStorageAccess::isIgnored() const
{
	return m_device->isDeviceBlacklisted();
}

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

	if (m_device->prop("IdUsage").toString() == "crypto")
		return requestPassphrase();
	else
		return mount();
}

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

	return unmount();
}

void UDisksStorageAccess::slotChanged()
{
	const bool old_isAccessible = m_isAccessible;
	updateCache();

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

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

void UDisksStorageAccess::slotDBusReply(const QDBusMessage& reply)
{
	Q_UNUSED(reply);
	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");
		}
	}
	else if (m_teardownInProgress) {
		QString clearTextPath = m_device->prop("LuksHolder").value<QDBusObjectPath>().path();
		if (isLuksDevice() && clearTextPath != "/")// unlocked device, lock it
		{
			callCryptoTeardown();
		}
		else if (m_device->prop("DeviceIsLuksCleartext").toBool()) {
			callCryptoTeardown(true);// Lock crypted parent
		}
		else {
			if (m_device->prop("DriveIsMediaEjectable").toBool() && m_device->prop("DeviceIsMediaAvailable").toBool() && !m_device->prop("DeviceIsOpticalDisc").toBool())// optical drives have their Eject method
			{
				QString devnode = m_device->prop("DeviceFile").toString();

#if defined(Q_OS_OPENBSD)
				QString program = "cdio";
				QStringList args;
				args << "-f" << devnode << "eject";
#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
				devnode.remove("/dev/").replace("([0-9]).", "\\1");
				QString program = "cdcontrol";
				QStringList args;
				args << "-f" << devnode << "eject";
#else
				QString program = "eject";
				QStringList args;
				args << devnode;
#endif

				QProcess::startDetached(program, args);
			}

			// try to eject the (parent) drive, e.g. SD card from a reader
			QString drivePath = m_device->prop("PartitionSlave").value<QDBusObjectPath>().path();
			if (!drivePath.isEmpty() || drivePath != "/") {
				QDBusConnection c = QDBusConnection::systemBus();
				QDBusMessage msg = QDBusMessage::createMethodCall(UD_DBUS_SERVICE, drivePath, UD_DBUS_INTERFACE_DISKS_DEVICE, "DriveEject");
				msg << QStringList();// options, unused now
				c.call(msg, QDBus::NoBlock);

				// power down removable USB hard drives, rhbz#852196
				UDisksDevice drive(drivePath);
				if (drive.prop("DriveCanDetach").toBool()) {
					QDBusMessage msg2 = QDBusMessage::createMethodCall(UD_DBUS_SERVICE, drivePath, UD_DBUS_INTERFACE_DISKS_DEVICE, "DriveDetach");
					msg2 << QStringList();// options, unused now
					c.call(msg2, QDBus::NoBlock);
				}
			}

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

void UDisksStorageAccess::slotDBusError(const QDBusError& error)
{
	if (m_setupInProgress) {
		m_setupInProgress = false;
		m_device->broadcastActionDone("setup", m_device->errorToSolidError(error.name()),
		                              m_device->errorToString(error.name()) + ": " + error.message());
	}
	else if (m_teardownInProgress) {
		m_teardownInProgress = false;
		m_device->broadcastActionDone("teardown", m_device->errorToSolidError(error.name()),
		                              m_device->errorToString(error.name()) + ": " + error.message());
	}
}

void UDisksStorageAccess::slotSetupRequested()
{
	m_setupInProgress = true;
	emit setupRequested(m_device->udi());
}

void UDisksStorageAccess::slotSetupDone(int error, const QString& errorString)
{
	m_setupInProgress = false;
	emit setupDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());
	slotChanged();
}

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

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

bool UDisksStorageAccess::mount()
{
	QString path = m_device->udi();
	if (path.endsWith(":media")) {
		path.chop(6);
	}
	QString fstype;
	QStringList options;

	if (isLuksDevice()) {// mount options for the cleartext volume
		path = m_device->prop("LuksHolder").value<QDBusObjectPath>().path();
		UDisksDevice holderDevice(path);
		fstype = holderDevice.prop("IdType").toString();
	}

	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD_DBUS_SERVICE, path, UD_DBUS_INTERFACE_DISKS_DEVICE, "FilesystemMount");

	if (m_device->prop("IdUsage").toString() == "filesystem")
		fstype = m_device->prop("IdType").toString();

	if (fstype == "vfat") {
		options << "flush";
	}

	msg << fstype;
	msg << options;

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

bool UDisksStorageAccess::unmount()
{
	QString path = m_device->udi();
	if (path.endsWith(":media")) {
		path.chop(6);
	}

	if (isLuksDevice()) {// unmount options for the cleartext volume
		path = m_device->prop("LuksHolder").value<QDBusObjectPath>().path();
	}

	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD_DBUS_SERVICE, path, UD_DBUS_INTERFACE_DISKS_DEVICE, "FilesystemUnmount");

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

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

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

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

bool UDisksStorageAccess::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 != 0)
		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 UDisksStorageAccess::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 UDisksStorageAccess::callCryptoSetup(const QString& passphrase)
{
	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD_DBUS_SERVICE, m_device->udi(), UD_DBUS_INTERFACE_DISKS_DEVICE, "LuksUnlock");

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

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

bool UDisksStorageAccess::callCryptoTeardown(bool actOnParent)
{
	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall(UD_DBUS_SERVICE,
	                                                  actOnParent ? (m_device->prop("LuksCleartextSlave").value<QDBusObjectPath>().path()) : m_device->udi(),
	                                                  UD_DBUS_INTERFACE_DISKS_DEVICE, "LuksLock");
	msg << QStringList();// options, unused now

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

#include "backends/udisks/moc_udisksstorageaccess.cpp"