Skip to content

Commit 0af02e3

Browse files
Merge pull request #974 from vojtechtrefny/master_crypttab-fix-no-keyfile
Fix parsing key file from /etc/crypttab
2 parents 69a9ace + c9305d7 commit 0af02e3

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/tests/dbus-tests/test_50_block.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,45 @@ def test_configuration_crypttab_multiple_spaces(self):
310310
self.assertEqual(conf.value[0][1]['name'], self.str_to_ay(self.vdevs[0]))
311311
self.assertEqual(conf.value[0][1]['device'], self.str_to_ay('UUID=%s' % uuid.value))
312312

313+
@udiskstestcase.tag_test(udiskstestcase.TestTags.UNSAFE)
314+
def test_configuration_crypttab_no_keyfile(self):
315+
# this test will change /etc/crypttab, we might want to revert the changes when it finishes
316+
crypttab = self.read_file('/etc/crypttab')
317+
self.addCleanup(self.write_file, '/etc/crypttab', crypttab)
318+
319+
# format the disk
320+
disk1 = self.get_object('/block_devices/' + os.path.basename(self.vdevs[0]))
321+
disk1.Format('xfs', {'encrypt.passphrase': 'test'}, dbus_interface=self.iface_prefix + '.Block')
322+
323+
# cleanup -- close the luks and remove format
324+
self.addCleanup(self.wipe_fs, self.vdevs[0])
325+
self.addCleanup(self._close_luks, disk1)
326+
327+
# format the disk
328+
disk2 = self.get_object('/block_devices/' + os.path.basename(self.vdevs[1]))
329+
disk2.Format('xfs', {'encrypt.passphrase': 'test'}, dbus_interface=self.iface_prefix + '.Block')
330+
331+
# cleanup -- close the luks and remove format
332+
self.addCleanup(self.wipe_fs, self.vdevs[1])
333+
self.addCleanup(self._close_luks, disk2)
334+
335+
# write configuration to crypttab
336+
# both "none" and "-" should be accepted as an empty/non-existing key file
337+
uuid1 = self.get_property(disk1, '.Block', 'IdUUID')
338+
uuid2 = self.get_property(disk2, '.Block', 'IdUUID')
339+
self.write_file('/etc/crypttab', '%s UUID=%s\tnone\n%s UUID=%s\t-\n' % (self.vdevs[0], uuid1.value,
340+
self.vdevs[1], uuid2.value))
341+
342+
# get the secret configuration (passphrase)
343+
sec_conf = disk1.GetSecretConfiguration(self.no_options, dbus_interface=self.iface_prefix + '.Block')
344+
self.assertIsNotNone(sec_conf)
345+
self.assertEqual(sec_conf[0][1]['passphrase-path'], self.str_to_ay(''))
346+
347+
# get the secret configuration (passphrase)
348+
sec_conf = disk2.GetSecretConfiguration(self.no_options, dbus_interface=self.iface_prefix + '.Block')
349+
self.assertIsNotNone(sec_conf)
350+
self.assertEqual(sec_conf[0][1]['passphrase-path'], self.str_to_ay(''))
351+
313352
def test_rescan(self):
314353

315354
disk = self.get_object('/block_devices/' + os.path.basename(self.vdevs[0]))

src/udiskslinuxblock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ add_crypttab_entry (GVariantBuilder *builder,
579579
gsize passphrase_contents_length;
580580

581581
passphrase_path = udisks_crypttab_entry_get_passphrase_path (entry);
582-
if (passphrase_path == NULL || g_strcmp0 (passphrase_path, "none") == 0)
582+
if (passphrase_path == NULL || g_strcmp0 (passphrase_path, "none") == 0 || g_strcmp0 (passphrase_path, "-") == 0)
583583
passphrase_path = "";
584584
passphrase_contents = NULL;
585585
if (!(g_strcmp0 (passphrase_path, "") == 0 || g_str_has_prefix (passphrase_path, "/dev")))
@@ -1774,7 +1774,7 @@ add_remove_crypttab_entry (UDisksBlock *block,
17741774
parsed_name, parsed_device, parsed_passphrase_path, parsed_options);
17751775
if (num_parsed >= 2)
17761776
{
1777-
if (num_parsed < 3 || g_strcmp0 (parsed_passphrase_path, "none") == 0)
1777+
if (num_parsed < 3 || g_strcmp0 (parsed_passphrase_path, "none") == 0 || g_strcmp0 (parsed_passphrase_path, "-") == 0)
17781778
strcpy (parsed_passphrase_path, "");
17791779
if (num_parsed < 4)
17801780
strcpy (parsed_options, "");

0 commit comments

Comments
 (0)