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

View File

@ -43,6 +43,17 @@ class PartitionRole;
namespace KPMHelpers
{
enum SavePassphraseValue
{
NoError,
EmptyPassphrase,
NotLuksPartition,
IncorrectPassphrase,
CryptsetupError,
NoMapperNode,
DeviceNotDecrypted
};
/**
* Iterates on all devices and return the first partition which is associated
* with mountPoint. This uses PartitionInfo::mountPoint(), not Partition::mountPoint()
@ -74,7 +85,7 @@ Partition* createNewEncryptedPartition( PartitionNode* parent,
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
*

View File

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