From 28535bf384dad0ae8e99352a83c3f605c42e871e Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Mon, 7 Oct 2024 23:37:33 +0200 Subject: [PATCH] Refactor generating of private key Generating of private key is atomic now. --- adminer/include/functions.inc.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 1e9755ae..a5ab480a 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -819,6 +819,13 @@ function file_write_unlock($fp, $data) { rewind($fp); fwrite($fp, $data); ftruncate($fp, strlen($data)); + file_unlock($fp); +} + +/** Unlock and close a file +* @param resource +*/ +function file_unlock($fp) { flock($fp, LOCK_UN); fclose($fp); } @@ -829,16 +836,19 @@ function file_write_unlock($fp, $data) { */ function password_file($create) { $filename = get_temp_dir() . "/adminer.key"; - $return = @file_get_contents($filename); // @ - may not exist - if ($return || !$create) { - return $return; + if (!$create && !file_exists($filename)) { + return false; } - $fp = @fopen($filename, "w"); // @ - can have insufficient rights //! is not atomic - if ($fp) { - chmod($filename, 0660); + $fp = file_open_lock($filename); + if (!$fp) { + return false; + } + $return = stream_get_contents($fp); + if (!$return) { $return = rand_string(); - fwrite($fp, $return); - fclose($fp); + file_write_unlock($fp, $return); + } else { + file_unlock($fp); } return $return; }