Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions emhttp/plugins/dynamix.plugin.manager/include/Downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,32 @@
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/webGui/include/Secure.php";
require_once __DIR__.'/ParityProtectedShrinkGuard.php';

// add translations
$_SERVER['REQUEST_URI'] = 'plugins';
require_once "$docroot/webGui/include/Translations.php";

$protectedShrinkFiles = parity_protected_shrink_files();
$downgradeMarker = '/var/run/unraid-os-downgrade';
$downgradeMarkerHandle = @fopen($downgradeMarker, 'x');

if ($downgradeMarkerHandle === false) {
http_response_code(409);
echo _('An Unraid downgrade is already pending.');
exit;
}

fclose($downgradeMarkerHandle);
chmod($downgradeMarker, 0600);

if (parity_protected_shrink_active($protectedShrinkFiles)) {
unlink($downgradeMarker);
http_response_code(409);
echo _('Finish or recover the active parity-protected disk removal before downgrading Unraid.');
exit;
}

$tmpdir="/boot/deletemedowngrade.".uniqid();
mkdir($tmpdir);
exec("mv -f /boot/bz* $tmpdir");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?PHP

function parity_protected_shrink_files($bootConfigRoot = '/boot/config') {
$bootConfigRoot = rtrim($bootConfigRoot, '/');

return [
"$bootConfigRoot/unraid/storage/parity-protected-shrink.json",
"$bootConfigRoot/storage/parity-protected-shrink-prepared",
"$bootConfigRoot/storage/parity-protected-shrink-prepared-complete",
"$bootConfigRoot/storage/parity-protected-shrink-daemon",
];
}

function parity_protected_shrink_active($protectedShrinkFiles) {
foreach ($protectedShrinkFiles as $protectedShrinkFile) {
if (is_file($protectedShrinkFile)) return true;
}

return false;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?PHP

require_once __DIR__.'/../include/ParityProtectedShrinkGuard.php';

function assert_same($expected, $actual, $message) {
if ($expected !== $actual) {
fwrite(STDERR, "$message\nExpected: ".var_export($expected, true)."\nActual: ".var_export($actual, true)."\n");
exit(1);
}
}

$productionFiles = parity_protected_shrink_files();
assert_same(
'/boot/config/unraid/storage/parity-protected-shrink.json',
$productionFiles[0],
'The downgrade guard must use the production Core intent path.'
);

$testRoot = sys_get_temp_dir().'/parity-protected-shrink-guard-'.bin2hex(random_bytes(8));
$testFiles = parity_protected_shrink_files($testRoot);
mkdir(dirname($testFiles[0]), 0700, true);
mkdir(dirname($testFiles[1]), 0700, true);

assert_same(false, parity_protected_shrink_active($testFiles), 'No proof must report idle.');
file_put_contents($testFiles[0], '{}');
assert_same(true, parity_protected_shrink_active($testFiles), 'A Core intent must block downgrade.');
unlink($testFiles[0]);
file_put_contents($testFiles[2], 'stage=prepared\n');
assert_same(true, parity_protected_shrink_active($testFiles), 'A daemon proof must block downgrade.');

unlink($testFiles[2]);
rmdir(dirname($testFiles[0]));
rmdir(dirname(dirname($testFiles[0])));
rmdir(dirname($testFiles[1]));
rmdir($testRoot);

echo "parity protected shrink downgrade guard: ok\n";

?>
Loading