1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-18 20:31:19 +02:00

Refactor generating of private key

Generating of private key is atomic now.
This commit is contained in:
Peter Knut
2024-10-07 23:37:33 +02:00
committed by Jakub Vrana
parent 43e3fe375d
commit 28535bf384

View File

@@ -819,6 +819,13 @@ function file_write_unlock($fp, $data) {
rewind($fp); rewind($fp);
fwrite($fp, $data); fwrite($fp, $data);
ftruncate($fp, strlen($data)); ftruncate($fp, strlen($data));
file_unlock($fp);
}
/** Unlock and close a file
* @param resource
*/
function file_unlock($fp) {
flock($fp, LOCK_UN); flock($fp, LOCK_UN);
fclose($fp); fclose($fp);
} }
@@ -829,16 +836,19 @@ function file_write_unlock($fp, $data) {
*/ */
function password_file($create) { function password_file($create) {
$filename = get_temp_dir() . "/adminer.key"; $filename = get_temp_dir() . "/adminer.key";
$return = @file_get_contents($filename); // @ - may not exist if (!$create && !file_exists($filename)) {
if ($return || !$create) { return false;
return $return;
} }
$fp = @fopen($filename, "w"); // @ - can have insufficient rights //! is not atomic $fp = file_open_lock($filename);
if ($fp) { if (!$fp) {
chmod($filename, 0660); return false;
}
$return = stream_get_contents($fp);
if (!$return) {
$return = rand_string(); $return = rand_string();
fwrite($fp, $return); file_write_unlock($fp, $return);
fclose($fp); } else {
file_unlock($fp);
} }
return $return; return $return;
} }