diff --git a/src/core/partitionrole.cpp b/src/core/partitionrole.cpp index 2805ce4..105ca16 100644 --- a/src/core/partitionrole.cpp +++ b/src/core/partitionrole.cpp @@ -1,40 +1,44 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * Copyright (C) 2016 by Andrius Štikonas * + * Copyright (C) 2016 by Teo Mrnjavac * * * * 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 3 of * * the License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #include "core/partitionrole.h" #include #include /** @return the role as string */ QString PartitionRole::toString() const { if (roles() & Unallocated) return i18nc("@item partition role", "unallocated"); if (roles() & Logical) return i18nc("@item partition role", "logical"); if (roles() & Extended) return i18nc("@item partition role", "extended"); if (roles() & Primary) return i18nc("@item partition role", "primary"); + if (roles() & LUKS) + return i18nc("@item partition role", "LUKS"); + return i18nc("@item partition role", "none"); } diff --git a/src/core/partitionrole.h b/src/core/partitionrole.h index 1834148..90ef7cb 100644 --- a/src/core/partitionrole.h +++ b/src/core/partitionrole.h @@ -1,75 +1,76 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * + * Copyright (C) 2016 by Teo Mrnjavac * * * * 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 3 of * * the License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #if !defined(PARTITIONROLE__H) #define PARTITIONROLE__H #include "../util/libpartitionmanagerexport.h" #include class QString; /** A Partition's role. Each Partition has a PartitionRole: It can be primary, extended, logical or represent unallocated space on the Device. @author Volker Lanz */ class LIBKPMCORE_EXPORT PartitionRole { public: /** A Partition's role: What kind of Partition is it? */ enum Role { None = 0, /**< None at all */ Primary = 1, /**< Primary */ Extended = 2, /**< Extended */ Logical = 4, /**< Logical inside an extended */ Unallocated = 8, /**< No real Partition, just unallocated space */ - LUKS = 16, + LUKS = 16, /**< Encrypted partition with LUKS key management */ Any = 255 /**< In case we're looking for a Partition with a PartitionRole, any will do */ }; Q_DECLARE_FLAGS(Roles, Role) public: explicit PartitionRole(Roles r) : m_Roles(r) {} /**< Creates a new PartitionRole object */ Roles roles() const { return m_Roles; /**< @return the roles as bitfield */ } bool has(Role r) const { return roles() & r; /**< @param r the role to check @return true if the role is set */ } bool operator==(const PartitionRole& other) const { return m_Roles == other.m_Roles; /**< @param other object to compare with @return true if the same */ } bool operator!=(const PartitionRole& other) const { return !operator==(other); /**< @param other object to compare with @return true if not the same */ } QString toString() const; private: Roles m_Roles; }; Q_DECLARE_OPERATORS_FOR_FLAGS(PartitionRole::Roles) #endif diff --git a/src/fs/luks.cpp b/src/fs/luks.cpp index 1e99b8d..676f349 100644 --- a/src/fs/luks.cpp +++ b/src/fs/luks.cpp @@ -1,545 +1,545 @@ /************************************************************************* * Copyright (C) 2012 by Volker Lanz * * Copyright (C) 2013 by Andrius Štikonas * - * Copyright (C) 2015 by Teo Mrnjavac * + * Copyright (C) 2015-2016 by Teo Mrnjavac * * * * 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 3 of * * the License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #include "fs/luks.h" #include "fs/filesystemfactory.h" #include "gui/decryptluksdialog.h" #include "util/capacity.h" #include "util/externalcommand.h" #include #include #include #include #include #include namespace FS { FileSystem::CommandSupportType luks::m_GetUsed = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_GetLabel = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Create = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Grow = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Shrink = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Move = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Check = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Copy = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_Backup = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_SetLabel = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_UpdateUUID = FileSystem::cmdSupportNone; FileSystem::CommandSupportType luks::m_GetUUID = FileSystem::cmdSupportNone; luks::luks(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label) : FileSystem(firstsector, lastsector, sectorsused, label, FileSystem::Luks) , m_innerFs(nullptr) , m_isCryptOpen(false) , m_isMounted(false) { } luks::~luks() { delete m_innerFs; } void luks::init() { m_Create = findExternal(QStringLiteral("cryptsetup")) ? cmdSupportFileSystem : cmdSupportNone; m_UpdateUUID = findExternal(QStringLiteral("cryptsetup")) ? cmdSupportFileSystem : cmdSupportNone; m_Copy = cmdSupportCore; m_Move = cmdSupportCore; m_Backup = cmdSupportCore; m_GetUUID = findExternal(QStringLiteral("cryptsetup")) ? cmdSupportFileSystem : cmdSupportNone; } bool luks::create(Report& report, const QString& deviceNode) const { Q_ASSERT(m_innerFs); QPointer dlg = new DecryptLuksDialog(0, deviceNode); //TODO: parent widget instead of 0 if (dlg->exec() != QDialog::Accepted) { delete dlg; return false; } std::vector commands; commands.push_back(QStringLiteral("echo")); commands.push_back(QStringLiteral("cryptsetup")); std::vector args; args.push_back({ dlg->luksPassphrase().text() }); args.push_back({ QStringLiteral("-s"), QStringLiteral("512"), QStringLiteral("luksFormat"), deviceNode }); ExternalCommand createCmd(commands, args); if (!(createCmd.run(-1) && createCmd.exitCode() == 0)) return false; commands.clear(); commands.push_back(QStringLiteral("echo")); commands.push_back(QStringLiteral("cryptsetup")); args.clear(); args.push_back({ dlg->luksPassphrase().text() }); args.push_back({ QStringLiteral("luksOpen"), deviceNode, dlg->luksName().text() }); delete dlg; ExternalCommand openCmd(commands, args); if (!(openCmd.run(-1) && openCmd.exitCode() == 0)) return false; QString mapperNode = mapperName(deviceNode); if (mapperNode.isEmpty()) return false; m_innerFs->create(report, mapperNode); m_isCryptOpen = (m_innerFs != nullptr); if (m_isCryptOpen) return true; return false; } bool luks::supportToolFound() const { return // m_GetUsed != cmdSupportNone && // m_GetLabel != cmdSupportNone && // m_SetLabel != cmdSupportNone && m_Create != cmdSupportNone && // m_Check != cmdSupportNone && m_UpdateUUID != cmdSupportNone && // m_Grow != cmdSupportNone && // m_Shrink != cmdSupportNone && m_Copy != cmdSupportNone && m_Move != cmdSupportNone && m_Backup != cmdSupportNone && m_GetUUID != cmdSupportNone; } FileSystem::SupportTool luks::supportToolName() const { return SupportTool(QStringLiteral("cryptsetup"), QUrl(QStringLiteral("https://code.google.com/p/cryptsetup/"))); } qint64 luks::minCapacity() const { return 3 * Capacity::unitFactor(Capacity::Byte, Capacity::MiB); } QString luks::mountTitle() const { return i18nc("@title:menu", "Mount"); } QString luks::unmountTitle() const { return i18nc("@title:menu", "Unmount"); } QString luks::cryptOpenTitle() const { return i18nc("@title:menu", "Decrypt"); } QString luks::cryptCloseTitle() const { return i18nc("@title:menu", "Deactivate"); } bool luks::canMount(const QString& deviceNode) const { return m_isCryptOpen && !m_isMounted && m_innerFs && m_innerFs->canMount(mapperName(deviceNode)); } bool luks::canUnmount(const QString& deviceNode) const { return m_isCryptOpen && m_isMounted && m_innerFs && m_innerFs->canUnmount(mapperName(deviceNode)); } bool luks::isMounted() const { return m_isCryptOpen && m_isMounted; } void luks::setMounted(bool mounted) { m_isMounted = mounted; } bool luks::canCryptOpen(const QString&) const { return !m_isCryptOpen && !m_isMounted; } bool luks::canCryptClose(const QString&) const { return m_isCryptOpen && !m_isMounted; } bool luks::isCryptOpen() const { return m_isCryptOpen; } void luks::setCryptOpen(bool cryptOpen) { m_isCryptOpen = cryptOpen; } bool luks::cryptOpen(const QString& deviceNode) { if (m_isCryptOpen) { if (!mapperName(deviceNode).isEmpty()) { qWarning() << "LUKS device" << deviceNode << "already decrypted." << "Cannot decrypt again."; return false; } else { qWarning() << "LUKS device" << deviceNode << "reportedly decrypted but mapper node not found." << "Marking device as NOT decrypted and trying to " "decrypt again anyway."; m_isCryptOpen = false; } } QPointer dlg = new DecryptLuksDialog(0, deviceNode); //TODO: parent widget instead of 0 if (dlg->exec() != QDialog::Accepted) { delete dlg; return false; } std::vector commands; commands.push_back(QStringLiteral("echo")); commands.push_back(QStringLiteral("cryptsetup")); std::vector args; args.push_back({ dlg->luksPassphrase().text() }); args.push_back({ QStringLiteral("luksOpen"), deviceNode, dlg->luksName().text() }); delete dlg; ExternalCommand cmd(commands, args); if (!(cmd.run(-1) && cmd.exitCode() == 0)) return false; if (m_innerFs) { delete m_innerFs; m_innerFs = nullptr; } QString mapperNode = mapperName(deviceNode); if (mapperNode.isEmpty()) return false; loadInnerFileSystem(mapperNode); m_isCryptOpen = (m_innerFs != nullptr); if (m_isCryptOpen) return true; return false; } bool luks::cryptClose(const QString& deviceNode) { if (!m_isCryptOpen) { qWarning() << "Cannot close LUKS device" << deviceNode << "because it's not open."; return false; } if (m_isMounted) { qWarning() << "Cannot close LUKS device" << deviceNode << "because the filesystem is mounted."; return false; } ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksClose"), mapperName(deviceNode) }); if (!(cmd.run(-1) && cmd.exitCode() == 0)) return false; delete m_innerFs; m_innerFs = nullptr; m_isCryptOpen = (m_innerFs != nullptr); if (!m_isCryptOpen) return true; return false; } void luks::loadInnerFileSystem(const QString& mapperNode) { Q_ASSERT(!m_innerFs); FileSystem::Type innerFsType = detectFileSystem(mapperNode); m_innerFs = FileSystemFactory::cloneWithNewType(innerFsType, *this); } void luks::createInnerFileSystem(FileSystem::Type type) { Q_ASSERT(!m_innerFs); m_innerFs = FileSystemFactory::cloneWithNewType(type, *this); } bool luks::mount(const QString& deviceNode, const QString& mountPoint) { if (!m_isCryptOpen) { qWarning() << "Cannot mount device" << deviceNode << "before decrypting it first."; return false; } if (m_isMounted) { qWarning() << "Cannot mount device" << deviceNode << "because it's already mounted."; return false; } Q_ASSERT(m_innerFs); QString mapperNode = mapperName(deviceNode); if (mapperNode.isEmpty()) return false; if (m_innerFs->canMount(mapperNode)) { if (m_innerFs->mount(mapperNode, mountPoint)) { m_isMounted = true; return true; } } else { ExternalCommand mountCmd( QStringLiteral("mount"), { QStringLiteral("-v"), mapperNode, mountPoint }); if (mountCmd.run() && mountCmd.exitCode() == 0) { m_isMounted = true; return true; } } return false; } bool luks::unmount(const QString& deviceNode) { if (!m_isCryptOpen) { qWarning() << "Cannot unmount device" << deviceNode << "before decrypting it first."; return false; } if (!m_isMounted) { qWarning() << "Cannot unmount device" << deviceNode << "because it's not mounted."; return false; } Q_ASSERT(m_innerFs); QString mapperNode = mapperName(deviceNode); if (mapperNode.isEmpty()) return false; if (m_innerFs->canUnmount(mapperNode)) { if (m_innerFs->unmount(mapperNode)) { m_isMounted = false; return true; } } else { ExternalCommand unmountCmd( QStringLiteral("mount"), { QStringLiteral("-v"), QStringLiteral("-A"), mapperNode }); if (unmountCmd.run() && unmountCmd.exitCode() == 0) { m_isMounted = false; return true; } } return false; } FileSystem::Type luks::type() const { if (m_isCryptOpen && m_innerFs) return m_innerFs->type(); return FileSystem::Luks; } QString luks::readUUID(const QString& deviceNode) const { ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksUUID"), deviceNode }); if (cmd.run()) { return cmd.output().simplified(); } return QStringLiteral("---"); } bool luks::updateUUID(Report& report, const QString& deviceNode) const { QUuid uuid = QUuid::createUuid(); ExternalCommand cmd(report, QStringLiteral("cryptsetup"), { QStringLiteral("luksUUID"), deviceNode, QStringLiteral("--uuid"), uuid.toString() }); return cmd.run(-1) && cmd.exitCode() == 0; } QString luks::mapperName(const QString& deviceNode) { ExternalCommand cmd(QStringLiteral("find"), { QStringLiteral("/dev/mapper/"), QStringLiteral("-exec"), QStringLiteral("cryptsetup"), QStringLiteral("status"), QStringLiteral("{}"), QStringLiteral(";") }); if (cmd.run()) { QRegExp rxDeviceName(QStringLiteral("(/dev/mapper/[A-Za-z0-9-/]+) is " "active[A-Za-z0-9- \\.\n]+[A-Za-z0-9-: \n]+") + deviceNode); if (rxDeviceName.indexIn(cmd.output()) > -1) return rxDeviceName.cap(1); } return QString(); } QString luks::getCipherName(const QString& deviceNode) { ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksDump"), deviceNode }); if (cmd.run()) { QRegExp rxCipherName(QStringLiteral("(?:Cipher name:\\s+)([A-Za-z0-9-]+)")); if (rxCipherName.indexIn(cmd.output()) > -1) return rxCipherName.cap(1); } return QStringLiteral("---"); } QString luks::getCipherMode(const QString& deviceNode) { ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksDump"), deviceNode }); if (cmd.run()) { QRegExp rxCipherMode(QStringLiteral("(?:Cipher mode:\\s+)([A-Za-z0-9-]+)")); if (rxCipherMode.indexIn(cmd.output()) > -1) return rxCipherMode.cap(1); } return QStringLiteral("---"); } QString luks::getHashName(const QString& deviceNode) { ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksDump"), deviceNode }); if (cmd.run()) { QRegExp rxHash(QStringLiteral("(?:Hash spec:\\s+)([A-Za-z0-9-]+)")); if (rxHash.indexIn(cmd.output()) > -1) return rxHash.cap(1); } return QStringLiteral("---"); } QString luks::getKeySize(const QString& deviceNode) { ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksDump"), deviceNode }); if (cmd.run()) { QRegExp rxKeySize(QStringLiteral("(?:MK bits:\\s+)(\\d+)")); if (rxKeySize.indexIn(cmd.output()) > -1) return rxKeySize.cap(1); } return QStringLiteral("---"); } QString luks::getPayloadOffset(const QString& deviceNode) { ExternalCommand cmd(QStringLiteral("cryptsetup"), { QStringLiteral("luksDump"), deviceNode }); if (cmd.run()) { QRegExp rxPayloadOffset(QStringLiteral("(?:Payload offset:\\s+)(\\d+)")); if (rxPayloadOffset.indexIn(cmd.output()) > -1) return rxPayloadOffset.cap(1); } return QStringLiteral("---"); } bool luks::canEncryptType(FileSystem::Type type) { switch (type) { case Ext2: case Ext3: case Ext4: case LinuxSwap: case ReiserFS: case Reiser4: case Xfs: case Jfs: case Btrfs: case Zfs: case Lvm2_PV: return true; default: return false; } } } diff --git a/src/fs/luks.h b/src/fs/luks.h index 478c7ef..45c48c2 100644 --- a/src/fs/luks.h +++ b/src/fs/luks.h @@ -1,150 +1,150 @@ /************************************************************************* * Copyright (C) 2012 by Volker Lanz * * Copyright (C) 2013 by Andrius Štikonas * - * Copyright (C) 2015 by Teo Mrnjavac * + * Copyright (C) 2015-2016 by Teo Mrnjavac * * * * 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 3 of * * the License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #if !defined(LUKS__H) #define LUKS__H #include "../util/libpartitionmanagerexport.h" #include "../fs/filesystem.h" #include #include class Report; class QString; namespace FS { /** A LUKS crypto file system. @author Andrius Štikonas */ class LIBKPMCORE_EXPORT luks : public FileSystem { public: luks(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label); virtual ~luks(); public: static void init(); virtual CommandSupportType supportGetUsed() const { return m_GetUsed; } virtual CommandSupportType supportGetLabel() const { return m_GetLabel; } virtual CommandSupportType supportCreate() const { return m_Create; } virtual CommandSupportType supportGrow() const { return m_Grow; } virtual CommandSupportType supportShrink() const { return m_Shrink; } virtual CommandSupportType supportMove() const { return m_Move; } virtual CommandSupportType supportCheck() const { return m_Check; } virtual CommandSupportType supportCopy() const { return m_Copy; } virtual CommandSupportType supportBackup() const { return m_Backup; } virtual CommandSupportType supportSetLabel() const { return m_SetLabel; } virtual CommandSupportType supportUpdateUUID() const { return m_UpdateUUID; } virtual CommandSupportType supportGetUUID() const { return m_GetUUID; } virtual bool create(Report &report, const QString &deviceNode) const override; virtual qint64 minCapacity() const; virtual SupportTool supportToolName() const; virtual bool supportToolFound() const; virtual QString readUUID(const QString& deviceNode) const; virtual bool updateUUID(Report& report, const QString& deviceNode) const; virtual QString mountTitle() const override; virtual QString unmountTitle() const override; QString cryptOpenTitle() const; QString cryptCloseTitle() const; virtual bool canMount(const QString&) const; virtual bool canUnmount(const QString&) const; bool isMounted() const; void setMounted(bool mounted); bool canCryptOpen(const QString& deviceNode) const; bool canCryptClose(const QString& deviceNode) const; bool isCryptOpen() const; void setCryptOpen(bool cryptOpen); bool cryptOpen(const QString& deviceNode); bool cryptClose(const QString& deviceNode); void loadInnerFileSystem(const QString& mapperNode); void createInnerFileSystem(Type type); virtual bool mount(const QString& deviceNode, const QString& mountPoint) override; virtual bool unmount(const QString& deviceNode) override; virtual FileSystem::Type type() const override; static QString mapperName(const QString& deviceNode); static QString getCipherName(const QString& deviceNode); static QString getCipherMode(const QString& deviceNode); static QString getHashName(const QString& deviceNode); static QString getKeySize(const QString& deviceNode); static QString getPayloadOffset(const QString& deviceNode); static bool canEncryptType(FileSystem::Type type); public: static CommandSupportType m_GetUsed; static CommandSupportType m_GetLabel; static CommandSupportType m_Create; static CommandSupportType m_Grow; static CommandSupportType m_Shrink; static CommandSupportType m_Move; static CommandSupportType m_Check; static CommandSupportType m_Copy; static CommandSupportType m_Backup; static CommandSupportType m_SetLabel; static CommandSupportType m_UpdateUUID; static CommandSupportType m_GetUUID; private: mutable FileSystem* m_innerFs; mutable bool m_isCryptOpen; bool m_isMounted; }; } #endif