Browse Source

Added udisks2 support for unlocking/locking of encrypted disks.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
undefined
Michele Calgaro 2 months ago
parent
commit
79fb6ff464
Signed by: Michele Calgaro <michele.calgaro@yahoo.it> GPG Key ID: 2A75B7CA8ADED5CF

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

@@ -33,7 +33,7 @@
//-------------------------------
// UDisks
//-------------------------------
TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
TQStringVariantMap udisksEjectDrive(TDEStorageDevice *sdevice) {
TQStringVariantMap result;
result["result"] = false;

@@ -65,7 +65,7 @@ TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice) {
return result;
}

TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions) {
TQStringVariantMap udisksMountDrive(const TQString &deviceNode, const TQString &fileSystemType, TQStringList mountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -106,7 +106,7 @@ TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType
return result;
}

TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions) {
TQStringVariantMap udisksUnmountDrive(const TQString &deviceNode, TQStringList unmountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -149,7 +149,7 @@ TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountO
//-------------------------------
// UDisks2
//-------------------------------
TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
TQStringVariantMap udisks2EjectDrive(TDEStorageDevice *sdevice) {
TQStringVariantMap result;
result["result"] = false;

@@ -218,7 +218,7 @@ TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice) {
return result;
}

TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions) {
TQStringVariantMap udisks2MountDrive(const TQString &deviceNode, const TQString &fileSystemType, const TQString &mountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -263,7 +263,7 @@ TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemTyp
return result;
}

TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions) {
TQStringVariantMap udisks2UnmountDrive(const TQString &deviceNode, const TQString &unmountOptions) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;
@@ -305,4 +305,94 @@ TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOpti
return result;
}

TQStringVariantMap udisks2UnlockDrive(const TQString &deviceNode, const TQString &passphrase) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;

TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) {
TQString blockDeviceString = deviceNode;
blockDeviceString.replace("/dev/", "");
blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;

// Unlock/decrypt the drive!
TQT_DBusError error;
TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
if (driveControl.canSend()) {
TQValueList<TQT_DBusData> params;
params << TQT_DBusData::fromString(passphrase);
TQMap<TQString, TQT_DBusVariant> optionsMap;
params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
TQT_DBusMessage reply = driveControl.sendWithReply("Unlock", params, &error);
if (!error.isValid()) {
if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
TQT_DBusObjectPath deviceObjectPath = reply[0].toObjectPath();
if (deviceObjectPath.isValid()) {
// Success
result["unlockedDevice"] = deviceObjectPath;
result["retcode"] = 0;
result["result"] = true;
return result;
}
}
result["errStr"] = i18n("Unknown error during unlocking operation.");
result["retcode"] = -1;
return result;
}
else {
// Error!
if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
return result; // Service not installed or unavailable
}
else {
result["errStr"] = error.name() + ": " + error.message();
result["retcode"] = -1;
return result;
}
}
}
}
return result;
}

TQStringVariantMap udisks2LockDrive(const TQString &deviceNode) {
TQStringVariantMap result;
result["result"] = false;
result["retcode"] = -2;

TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
if (dbusConn.isConnected()) {
TQString blockDeviceString = deviceNode;
blockDeviceString.replace("/dev/", "");
blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;

// Lock/encrypt the drive!
TQT_DBusError error;
TQT_DBusProxy driveControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.UDisks2.Encrypted", dbusConn);
if (driveControl.canSend()) {
TQValueList<TQT_DBusData> params;
TQMap<TQString, TQT_DBusVariant> optionsMap;
params << TQT_DBusData::fromStringKeyMap(TQT_DBusDataMap<TQString>(optionsMap));
TQT_DBusMessage reply = driveControl.sendWithReply("Lock", params, &error);
if (!error.isValid()) {
// Success
result["retcode"] = 0;
result["result"] = true;
return result;
}
else {
// Error!
if (error.name() == "org.freedesktop.DBus.Error.ServiceUnknown") {
return result; // Service not installed or unavailable
}
else {
result["errStr"] = error.name() + ": " + error.message();
result["retcode"] = -1;
return result;
}
}
}
}
return result;
}

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

@@ -27,15 +27,17 @@
//-------------------------------
// UDisks
//-------------------------------
TQStringVariantMap UDisksEjectDrive(TDEStorageDevice *sdevice);
TQStringVariantMap UDisksMountDrive(TQString deviceNode, TQString fileSystemType, TQStringList mountOptions);
TQStringVariantMap UDisksUnmountDrive(TQString deviceNode, TQStringList unmountOptions);
TQStringVariantMap udisksEjectDrive(TDEStorageDevice *sdevice);
TQStringVariantMap udisksMountDrive(const TQString &deviceNode, const TQString &fileSystemType, TQStringList mountOptions);
TQStringVariantMap udisksUnmountDrive(const TQString &deviceNode, TQStringList unmountOptions);

//-------------------------------
// UDisks2
//-------------------------------
TQStringVariantMap UDisks2EjectDrive(TDEStorageDevice *sdevice);
TQStringVariantMap UDisks2MountDrive(TQString deviceNode, TQString fileSystemType, TQString mountOptions);
TQStringVariantMap UDisks2UnmountDrive(TQString deviceNode, TQString unmountOptions);
TQStringVariantMap udisks2EjectDrive(TDEStorageDevice *sdevice);
TQStringVariantMap udisks2MountDrive(const TQString &deviceNode, const TQString &fileSystemType, const TQString &mountOptions);
TQStringVariantMap udisks2UnmountDrive(const TQString &deviceNode, const TQString &unmountOptions);
TQStringVariantMap udisks2UnlockDrive(const TQString &deviceNode, const TQString &passphrase);
TQStringVariantMap udisks2LockDrive(const TQString &deviceNode);

#endif

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

@@ -309,7 +309,7 @@ bool TDEStorageDevice::lockDriveMedia(bool lock) {

bool TDEStorageDevice::ejectDrive() {
if (!(TDEGlobal::dirs()->findExe("udisksctl").isEmpty())) {
TQStringVariantMap ejectResult = UDisks2EjectDrive(this);
TQStringVariantMap ejectResult = udisks2EjectDrive(this);
if (ejectResult["result"].toBool()) {
return true;
}
@@ -319,7 +319,7 @@ bool TDEStorageDevice::ejectDrive() {
}
}
if (!(TDEGlobal::dirs()->findExe("udisks").isEmpty())) {
TQStringVariantMap ejectResult = UDisksEjectDrive(this);
TQStringVariantMap ejectResult = udisksEjectDrive(this);
if (ejectResult["result"].toBool()) {
return true;
}
@@ -773,7 +773,7 @@ TQStringVariantMap TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageM
fileSystemType = mountOptions["filesystem"];
}

TQStringVariantMap mountResult = UDisks2MountDrive(devNode, fileSystemType, optionString);
TQStringVariantMap mountResult = udisks2MountDrive(devNode, fileSystemType, optionString);
if (mountResult["result"].toBool()) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -791,7 +791,7 @@ TQStringVariantMap TDEStorageDevice::mountDevice(TQString mediaName, TDEStorageM

// The UDISKS v2 DBUS service was either not available or was unusable
// Try to use UDISKS v1 via DBUS, if available
mountResult = UDisksMountDrive(devNode, fileSystemType, udisksOptions);
mountResult = udisksMountDrive(devNode, fileSystemType, udisksOptions);
if (mountResult["result"].toBool()) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -989,7 +989,7 @@ TQStringVariantMap TDEStorageDevice::unmountDevice() {
TQString devNode = deviceNode();

// Try to use UDISKS v2 via DBUS, if available
TQStringVariantMap unmountResult = UDisks2UnmountDrive(devNode, TQString::null);
TQStringVariantMap unmountResult = udisks2UnmountDrive(devNode, TQString::null);
if (unmountResult["result"].toBool()) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -1006,7 +1006,7 @@ TQStringVariantMap TDEStorageDevice::unmountDevice() {

// The UDISKS v2 DBUS service was either not available or was unusable
// Try to use UDISKS v1 via DBUS, if available
unmountResult = UDisksUnmountDrive(devNode, TQStringList());
unmountResult = udisksUnmountDrive(devNode, TQStringList());
if (unmountResult["result"].toBool()) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
@@ -1063,6 +1063,81 @@ TQStringVariantMap TDEStorageDevice::unmountDevice() {
return result;
}

TQStringVariantMap TDEStorageDevice::unlockDevice(const TQString &passphrase)
{
TQStringVariantMap result;

// Check if device is already mounted
TQString mountpath = mountPath();
if (!mountpath.isEmpty()) {
result["unlockedDevice"] = mountpath;
result["result"] = true;
return result;
}

TQString devNode = deviceNode();
devNode.replace("'", "'\\''");

// Try to use UDISKS v2 via DBUS, if available
TQStringVariantMap unlockResult = udisks2UnlockDrive(devNode, passphrase);
if (unlockResult["result"].toBool()) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
result["unlockedDevice"] = unlockResult["unlockedDevice"];
result["result"] = true;
return result;
}
else if (unlockResult["retcode"].toInt() == -1) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
result["errStr"] = unlockResult["errStr"];
result["result"] = false;
return result;
}

// No supported methods found for unlocking the device
result["errStr"] = i18n("No supported unlocking methods were detected on your system.");
result["result"] = false;
return result;
}

TQStringVariantMap TDEStorageDevice::lockDevice()
{
TQStringVariantMap result;

// Check if device is mounted
TQString mountpath = mountPath();
if (!mountpath.isEmpty()) {
result["errStr"] = i18n("The device is currently mounted and cannot be locked.");
result["result"] = false;
return result;
}

TQString devNode = deviceNode();
devNode.replace("'", "'\\''");

// Try to use UDISKS v2 via DBUS, if available
TQStringVariantMap lockResult = udisks2LockDrive(devNode);
if (lockResult["result"].toBool()) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
result["result"] = true;
return result;
}
else if (lockResult["retcode"].toInt() == -1) {
// Update internal mount data
TDEGlobal::hardwareDevices()->processModifiedMounts();
result["errStr"] = lockResult["errStr"];
result["result"] = false;
return result;
}

// No supported methods found for locking the device
result["errStr"] = i18n("No supported locking methods were detected on your system.");
result["result"] = false;
return result;
}

TQString TDEStorageDevice::determineFileSystemType(TQString path) {
TQStringList mountTable;
TQString prevPath = path;

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

@@ -259,6 +259,22 @@ class TDECORE_EXPORT TDEStorageDevice : public TDEGenericDevice
TQStringVariantMap unmountDevice();

/**
* Unlock/decrypt an encrypted storage device
*
* @param a TQString containing the passphrase
*
* @return a TQStringVariantMap object containing information about the operation outcome
*/
TQStringVariantMap unlockDevice(const TQString &passphrase);

/**
* Lock/encrypt an encrypted storage device
*
* @return a TQStringVariantMap object containing information about the operation outcome
*/
TQStringVariantMap lockDevice();

/**
* @return a TQString with the mount path, if mounted
*/
TQString mountPath();

Loading…
Cancel
Save