[partition] update LuksDevice to savePassphrase and enum

This commit is contained in:
abalfoort 2022-05-19 20:18:10 +02:00 committed by Adriaan de Groot
parent 4edad4d8c4
commit 2a7cbd2520
3 changed files with 32 additions and 22 deletions

View File

@ -167,24 +167,27 @@ testPassphrase( FS::luks* fs, const QString& deviceNode, const QString& passphra
} }
#endif #endif
// Adapted from luks cryptOpen which always opens a dialog to ask for a passphrase // Adapted from src/fs/luks.cpp cryptOpen which always opens a dialog to ask for a passphrase
int /**
updateLuksDevice( Partition* partition, const QString& passphrase ) * @brief
* Save an existing passphrase for a previously encrypted partition.
* @param partition
* @param passphrase
* @return SavePassphraseValue
*/
SavePassphraseValue
savePassphrase( Partition* partition, const QString& passphrase )
{ {
const QString deviceNode = partition->partitionPath(); const QString deviceNode = partition->partitionPath();
cDebug() << "Update Luks device: " << deviceNode;
if ( passphrase.isEmpty() ) if ( passphrase.isEmpty() )
{ {
cWarning() << Logger::SubEntry << "#1: Passphrase is empty"; return SavePassphraseValue::EmptyPassphrase;
return 1;
} }
if ( partition->fileSystem().type() != FileSystem::Luks ) if ( partition->fileSystem().type() != FileSystem::Luks )
{ {
cWarning() << Logger::SubEntry << "#2: Not a luks encrypted device"; return SavePassphraseValue::NotLuksPartition;
return 2;
} }
// Cast partition fs to luks fs // Cast partition fs to luks fs
@ -193,16 +196,14 @@ updateLuksDevice( Partition* partition, const QString& passphrase )
// Test the given passphrase // Test the given passphrase
if ( !testPassphrase( luksFs, deviceNode, passphrase ) ) if ( !testPassphrase( luksFs, deviceNode, passphrase ) )
{ {
cWarning() << Logger::SubEntry << "#3: Passphrase incorrect"; return SavePassphraseValue::IncorrectPassphrase;
return 3;
} }
if ( luksFs->isCryptOpen() ) if ( luksFs->isCryptOpen() )
{ {
if ( !luksFs->mapperName().isEmpty() ) if ( !luksFs->mapperName().isEmpty() )
{ {
cWarning() << Logger::SubEntry << "#4: Device already decrypted"; return SavePassphraseValue::NoError;
return 4;
} }
else else
{ {
@ -217,7 +218,7 @@ updateLuksDevice( Partition* partition, const QString& passphrase )
if ( !( openCmd.write( passphrase.toLocal8Bit() + '\n' ) && openCmd.start( -1 ) && openCmd.exitCode() == 0 ) ) if ( !( openCmd.write( passphrase.toLocal8Bit() + '\n' ) && openCmd.start( -1 ) && openCmd.exitCode() == 0 ) )
{ {
cWarning() << Logger::SubEntry << openCmd.exitCode() << ": cryptsetup command failed"; cWarning() << Logger::SubEntry << openCmd.exitCode() << ": cryptsetup command failed";
return openCmd.exitCode(); return SavePassphraseValue::CryptsetupError;
} }
// Save the existing passphrase // Save the existing passphrase
@ -227,8 +228,7 @@ updateLuksDevice( Partition* partition, const QString& passphrase )
if ( luksFs->mapperName().isEmpty() ) if ( luksFs->mapperName().isEmpty() )
{ {
cWarning() << Logger::SubEntry << "#5: No mapper node found"; return SavePassphraseValue::NoMapperNode;
return 5;
} }
luksFs->loadInnerFileSystem( luksFs->mapperName() ); luksFs->loadInnerFileSystem( luksFs->mapperName() );
@ -236,11 +236,10 @@ updateLuksDevice( Partition* partition, const QString& passphrase )
if ( !luksFs->isCryptOpen() ) if ( !luksFs->isCryptOpen() )
{ {
cWarning() << Logger::SubEntry << "#6: Device could not be decrypted"; return SavePassphraseValue::DeviceNotDecrypted;
return 6;
} }
return 0; return SavePassphraseValue::NoError;
} }
Calamares::JobResult Calamares::JobResult

View File

@ -43,6 +43,17 @@ class PartitionRole;
namespace KPMHelpers namespace KPMHelpers
{ {
enum SavePassphraseValue
{
NoError,
EmptyPassphrase,
NotLuksPartition,
IncorrectPassphrase,
CryptsetupError,
NoMapperNode,
DeviceNotDecrypted
};
/** /**
* Iterates on all devices and return the first partition which is associated * Iterates on all devices and return the first partition which is associated
* with mountPoint. This uses PartitionInfo::mountPoint(), not Partition::mountPoint() * with mountPoint. This uses PartitionInfo::mountPoint(), not Partition::mountPoint()
@ -74,7 +85,7 @@ Partition* createNewEncryptedPartition( PartitionNode* parent,
Partition* clonePartition( Device* device, Partition* partition ); Partition* clonePartition( Device* device, Partition* partition );
int updateLuksDevice( Partition* partition, const QString& passphrase ); SavePassphraseValue savePassphrase( Partition* partition, const QString& passphrase );
/** @brief Return a result for an @p operation /** @brief Return a result for an @p operation
* *

View File

@ -254,8 +254,8 @@ EditExistingPartitionDialog::applyChanges( PartitionCoreModule* core )
const QString passphrase = m_ui->encryptWidget->passphrase(); const QString passphrase = m_ui->encryptWidget->passphrase();
if ( !passphrase.isEmpty() ) if ( !passphrase.isEmpty() )
{ {
int retCode = KPMHelpers::updateLuksDevice( m_partition, passphrase ); KPMHelpers::SavePassphraseValue ret = KPMHelpers::savePassphrase( m_partition, passphrase );
if ( retCode != 0 ) if ( ret != KPMHelpers::SavePassphraseValue::NoError )
{ {
QString message = tr( "Passphrase for existing partition" ); QString message = tr( "Passphrase for existing partition" );
QString description = tr( "Partition %1 could not be decrypted " QString description = tr( "Partition %1 could not be decrypted "