Browse Source

Added udisks2 support for unlocking/locking of encrypted disks.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
undefined
Michele Calgaro 1 month ago
parent
commit
66db2c8c4e
Signed by: Michele Calgaro <michele.calgaro@yahoo.it> GPG Key ID: 2A75B7CA8ADED5CF

+ 96
- 6
tdecore/tdehw/disksHelper.cpp View File

@@ -33,7 +33,7 @@
33 33
 //-------------------------------
34 34
 //  UDisks
35 35
 //-------------------------------
36
-TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
36
+TQStringVariantMap udisksEjectDrive(TDEStorageDevice *sdevice) {
37 37
 	TQStringVariantMap result;
38 38
 	result["result"] = false;
39 39
 
@@ -65,7 +65,7 @@ TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
65 65
 	return result;
66 66
 }
67 67
 
68
-TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions) {
68
+TQStringVariantMap udisksMountDrive(const TQString &deviceNode, const TQString &fileSystemType, TQStringList mountOptions) {
69 69
 	TQStringVariantMap result;
70 70
 	result["result"] = false;
71 71
 	result["retcode"] = -2;
@@ -106,7 +106,7 @@ TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType
106 106
 	return result;
107 107
 }
108 108
 
109
-TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions) {
109
+TQStringVariantMap udisksUnmountDrive(const TQString &deviceNode, TQStringList unmountOptions) {
110 110
 	TQStringVariantMap result;
111 111
 	result["result"] = false;
112 112
 	result["retcode"] = -2;
@@ -149,7 +149,7 @@ TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountO
149 149
 //-------------------------------
150 150
 //  UDisks2
151 151
 //-------------------------------
152
-TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
152
+TQStringVariantMap udisks2EjectDrive(TDEStorageDevice *sdevice) {
153 153
 	TQStringVariantMap result;
154 154
 	result["result"] = false;
155 155
 
@@ -218,7 +218,7 @@ TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
218 218
 	return result;
219 219
 }
220 220
 
221
-TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions) {
221
+TQStringVariantMap udisks2MountDrive(const TQString &deviceNode, const TQString &fileSystemType, const TQString &mountOptions) {
222 222
 	TQStringVariantMap result;
223 223
 	result["result"] = false;
224 224
 	result["retcode"] = -2;
@@ -263,7 +263,7 @@ TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemTyp
263 263
 	return result;
264 264
 }
265 265
 
266
-TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions) {
266
+TQStringVariantMap udisks2UnmountDrive(const TQString &deviceNode, const TQString &unmountOptions) {
267 267
 	TQStringVariantMap result;
268 268
 	result["result"] = false;
269 269
 	result["retcode"] = -2;
@@ -305,4 +305,94 @@ TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOpti
305 305
 	return result;
306 306
 }
307 307
 
308
+TQStringVariantMap udisks2UnlockDrive(const TQString &deviceNode, const TQString &passphrase) {
309
+	TQStringVariantMap result;
310
+	result["result"] = false;
311
+	result["retcode"] = -2;
312
+
313
+	TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
314
+	if (dbusConn.isConnected()) {
315
+		TQString blockDeviceString = deviceNode;
316
+		blockDeviceString.replace("/dev/", "");
317
+		blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
318
+
319
+		// Unlock/decrypt the drive!
320
+		TQT_DBusError error;
321
+		TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
322
+		if (driveControl.canSend()) {
323
+			TQValueList<TQT_DBusData> params;
324
+			params << TQT_DBusData::fromString(passphrase);
325
+			TQMap<TQString, TQT_DBusVariant> optionsMap;
326
+			params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
327
+			TQT_DBusMessage reply = driveControl.sendWithReply("Unlock", params, &error);
328
+			if (!error.isValid()) {
329
+				if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
330
+					TQT_DBusObjectPath deviceObjectPath = reply[0].toObjectPath();
331
+					if (deviceObjectPath.isValid()) {
332
+						// Success
333
+						result["unlockedDevice"] = deviceObjectPath;
334
+						result["retcode"] = 0;
335
+						result["result"] = true;
336
+						return result;
337
+					}
338
+				}
339
+				result["errStr"] = i18n("Unknown error during unlocking operation.");
340
+				result["retcode"] = -1;
341
+				return result;
342
+			}
343
+			else {
344
+				// Error!
345
+				if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
346
+					return result;  // Service not installed or unavailable
347
+				}
348
+				else {
349
+					result["errStr"] = error.name() + ": " + error.message();
350
+					result["retcode"] = -1;
351
+					return result;
352
+				}
353
+			}
354
+		}
355
+	}
356
+	return result;
357
+}
358
+
359
+TQStringVariantMap udisks2LockDrive(const TQString &deviceNode) {
360
+	TQStringVariantMap result;
361
+	result["result"] = false;
362
+	result["retcode"] = -2;
363
+
364
+	TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
365
+	if (dbusConn.isConnected()) {
366
+		TQString blockDeviceString = deviceNode;
367
+		blockDeviceString.replace("/dev/", "");
368
+		blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
308 369
 
370
+		// Lock/encrypt the drive!
371
+		TQT_DBusError error;
372
+		TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
373
+		if (driveControl.canSend()) {
374
+			TQValueList<TQT_DBusData> params;
375
+			TQMap<TQString, TQT_DBusVariant> optionsMap;
376
+			params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
377
+			TQT_DBusMessage reply = driveControl.sendWithReply("Lock", params, &error);
378
+			if (!error.isValid()) {
379
+				// Success
380
+				result["retcode"] = 0;
381
+				result["result"] = true;
382
+				return result;
383
+			}
384
+			else {
385
+				// Error!
386
+				if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
387
+					return result;  // Service not installed or unavailable
388
+				}
389
+				else {
390
+					result["errStr"] = error.name() + ": " + error.message();
391
+					result["retcode"] = -1;
392
+					return result;
393
+				}
394
+			}
395
+		}
396
+	}
397
+	return result;
398
+}

+ 8
- 6
tdecore/tdehw/disksHelper.h View File

@@ -27,15 +27,17 @@
27 27
 //-------------------------------
28 28
 //  UDisks
29 29
 //-------------------------------
30
-TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice);
31
-TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions);
32
-TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions);
30
+TQStringVariantMap udisksEjectDrive(TDEStorageDevice *sdevice);
31
+TQStringVariantMap udisksMountDrive(const TQString &deviceNode, const TQString &fileSystemType, TQStringList mountOptions);
32
+TQStringVariantMap udisksUnmountDrive(const TQString &deviceNode, TQStringList unmountOptions);
33 33
 
34 34
 //-------------------------------
35 35
 //  UDisks2
36 36
 //-------------------------------
37
-TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice);
38
-TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions);
39
-TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions);
37
+TQStringVariantMap udisks2EjectDrive(TDEStorageDevice *sdevice);
38
+TQStringVariantMap udisks2MountDrive(const TQString &deviceNode, const TQString &fileSystemType, const TQString &mountOptions);
39
+TQStringVariantMap udisks2UnmountDrive(const TQString &deviceNode, const TQString &unmountOptions);
40
+TQStringVariantMap udisks2UnlockDrive(const TQString &deviceNode, const TQString &passphrase);
41
+TQStringVariantMap udisks2LockDrive(const TQString &deviceNode);
40 42
 
41 43
 #endif

+ 81
- 6
tdecore/tdehw/tdestoragedevice.cpp View File

@@ -309,7 +309,7 @@ bool TDEStorageDevice::lockDriveMedia(bool lock) {
309 309
 
310 310
 bool TDEStorageDevice::ejectDrive() {
311 311
 	if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) {
312
-		TQStringVariantMap ejectResult = UDisks2EjectDrive(this);
312
+		TQStringVariantMap ejectResult = udisks2EjectDrive(this);
313 313
 		if (ejectResult["result"].toBool()) {
314 314
 			return true;
315 315
 		}
@@ -319,7 +319,7 @@ bool TDEStorageDevice::ejectDrive() {
319 319
 		}
320 320
 	}
321 321
 	if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) {
322
-		TQStringVariantMap ejectResult = UDisksEjectDrive(this);
322
+		TQStringVariantMap ejectResult = udisksEjectDrive(this);
323 323
 		if (ejectResult["result"].toBool()) {
324 324
 			return true;
325 325
 		}
@@ -773,7 +773,7 @@ TQStringVariantMap TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageM
773 773
 		fileSystemType = mountOptions["filesystem"];
774 774
 	}
775 775
 
776
-	TQStringVariantMap mountResult = UDisks2MountDrive(devNode, fileSystemType, optionString);
776
+	TQStringVariantMap mountResult = udisks2MountDrive(devNode, fileSystemType, optionString);
777 777
 	if (mountResult["result"].toBool()) {
778 778
 		// Update internal mount data
779 779
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -791,7 +791,7 @@ TQStringVariantMap TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageM
791 791
 
792 792
 	// The UDISKS v2 DBUS service was either not available or was unusable
793 793
 	// Try to use UDISKS v1 via DBUS, if available
794
-	mountResult = UDisksMountDrive(devNode, fileSystemType, udisksOptions);
794
+	mountResult = udisksMountDrive(devNode, fileSystemType, udisksOptions);
795 795
 	if (mountResult["result"].toBool()) {
796 796
 		// Update internal mount data
797 797
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -989,7 +989,7 @@ TQStringVariantMap TDEStorageDevice::unmountDevice() {
989 989
 	TQString devNode = deviceNode();
990 990
 
991 991
 	// Try to use UDISKS v2 via DBUS, if available
992
-	TQStringVariantMap unmountResult = UDisks2UnmountDrive(devNode, TQString::null);
992
+	TQStringVariantMap unmountResult = udisks2UnmountDrive(devNode, TQString::null);
993 993
 	if (unmountResult["result"].toBool()) {
994 994
 		// Update internal mount data
995 995
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -1006,7 +1006,7 @@ TQStringVariantMap TDEStorageDevice::unmountDevice() {
1006 1006
 
1007 1007
 	// The UDISKS v2 DBUS service was either not available or was unusable
1008 1008
 	// Try to use UDISKS v1 via DBUS, if available
1009
-	unmountResult = UDisksUnmountDrive(devNode, TQStringList());
1009
+	unmountResult = udisksUnmountDrive(devNode, TQStringList());
1010 1010
 	if (unmountResult["result"].toBool()) {
1011 1011
 		// Update internal mount data
1012 1012
 		TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -1063,6 +1063,81 @@ TQStringVariantMap TDEStorageDevice::unmountDevice() {
1063 1063
 	return result;
1064 1064
 }
1065 1065
 
1066
+TQStringVariantMap TDEStorageDevice::unlockDevice(const TQString &passphrase)
1067
+{
1068
+	TQStringVariantMap result;
1069
+
1070
+	// Check if device is already mounted
1071
+	TQString mountpath = mountPath();
1072
+	if (!mountpath.isEmpty()) {
1073
+		result["unlockedDevice"] = mountpath;
1074
+		result["result"] = true;
1075
+		return result;
1076
+	}
1077
+
1078
+	TQString devNode = deviceNode();
1079
+	devNode.replace("'", "'\\''");
1080
+
1081
+	// Try to use UDISKS v2 via DBUS, if available
1082
+	TQStringVariantMap unlockResult = udisks2UnlockDrive(devNode, passphrase);
1083
+	if (unlockResult["result"].toBool()) {
1084
+		// Update internal mount data
1085
+		TDEGlobal::hardwareDevices()->processModifiedMounts();
1086
+		result["unlockedDevice"] = unlockResult["unlockedDevice"];
1087
+		result["result"] = true;
1088
+		return result;
1089
+	}
1090
+	else if (unlockResult["retcode"].toInt() == -1) {
1091
+		// Update internal mount data
1092
+		TDEGlobal::hardwareDevices()->processModifiedMounts();
1093
+		result["errStr"] = unlockResult["errStr"];
1094
+		result["result"] = false;
1095
+		return result;
1096
+	}
1097
+
1098
+	// No supported methods found for unlocking the device
1099
+	result["errStr"] = i18n("No supported unlocking methods were detected on your system.");
1100
+	result["result"] = false;
1101
+	return result;
1102
+}
1103
+
1104
+TQStringVariantMap TDEStorageDevice::lockDevice()
1105
+{
1106
+	TQStringVariantMap result;
1107
+
1108
+	// Check if device is mounted
1109
+	TQString mountpath = mountPath();
1110
+	if (!mountpath.isEmpty()) {
1111
+		result["errStr"] = i18n("The device is currently mounted and cannot be locked.");
1112
+		result["result"] = false;
1113
+		return result;
1114
+	}
1115
+
1116
+	TQString devNode = deviceNode();
1117
+	devNode.replace("'", "'\\''");
1118
+
1119
+	// Try to use UDISKS v2 via DBUS, if available
1120
+	TQStringVariantMap lockResult = udisks2LockDrive(devNode);
1121
+	if (lockResult["result"].toBool()) {
1122
+		// Update internal mount data
1123
+		TDEGlobal::hardwareDevices()->processModifiedMounts();
1124
+		result["result"] = true;
1125
+		return result;
1126
+	}
1127
+	else if (lockResult["retcode"].toInt() == -1) {
1128
+		// Update internal mount data
1129
+		TDEGlobal::hardwareDevices()->processModifiedMounts();
1130
+		result["errStr"] = lockResult["errStr"];
1131
+		result["result"] = false;
1132
+		return result;
1133
+	}
1134
+
1135
+	// No supported methods found for locking the device
1136
+	result["errStr"] = i18n("No supported locking methods were detected on your system.");
1137
+	result["result"] = false;
1138
+	return result;
1139
+}
1140
+
1066 1141
 TQString TDEStorageDevice::determineFileSystemType(TQString path) {
1067 1142
 	TQStringList mountTable;
1068 1143
 	TQString prevPath = path;

+ 16
- 0
tdecore/tdehw/tdestoragedevice.h View File

@@ -259,6 +259,22 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
259 259
 		TQStringVariantMap unmountDevice();
260 260
 
261 261
 		/**
262
+		 * Unlock/decrypt an encrypted storage device
263
+		 *
264
+		 * @param a TQString containing the passphrase
265
+		 *
266
+		 * @return a TQStringVariantMap object containing information about the operation outcome
267
+		 */
268
+		TQStringVariantMap unlockDevice(const TQString &passphrase);
269
+
270
+		/**
271
+		 * Lock/encrypt an encrypted storage device
272
+		 *
273
+		 * @return a TQStringVariantMap object containing information about the operation outcome
274
+		 */
275
+		TQStringVariantMap lockDevice();
276
+
277
+		/**
262 278
 		 * @return a TQString with the mount path, if mounted
263 279
 		 */
264 280
 		TQString mountPath();

Loading…
Cancel
Save