mirror of
https://github.com/vrana/adminer.git
synced 2025-08-16 19:44:00 +02:00
Refactor generating of private key
Generating of private key is atomic now.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user