mirror of
https://github.com/webmin/webmin.git
synced 2026-02-03 14:13:29 +00:00
Perfect previous code
This commit is contained in:
49
miniserv.pl
49
miniserv.pl
@@ -6,7 +6,7 @@ package miniserv;
|
||||
use Socket;
|
||||
use POSIX;
|
||||
use Time::Local;
|
||||
use Fcntl qw(LOCK_EX LOCK_UN);
|
||||
use Fcntl qw(:flock);
|
||||
eval "use Time::HiRes;";
|
||||
|
||||
@itoa64 = split(//, "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
|
||||
@@ -6193,17 +6193,17 @@ chmod(0700, $config{'blockedfile'});
|
||||
}
|
||||
|
||||
# update_wellknown_file(hostname)
|
||||
# Writes out a text file of well-known hosts
|
||||
# Writes out a text file of well-known hosts. Returns 1 on success, 0 on failure
|
||||
sub update_wellknown_file
|
||||
{
|
||||
my ($h) = @_;
|
||||
return if !$h;
|
||||
return 0 if !$h;
|
||||
|
||||
my $path = $config{'wellknown'};
|
||||
|
||||
my $lock = "$path.lock";
|
||||
open(my $lk, ">>", $lock) or return;
|
||||
flock($lk, 2);
|
||||
open(my $lk, '>>', $lock) or return 0;
|
||||
flock($lk, LOCK_EX);
|
||||
|
||||
# Read current set
|
||||
my %set;
|
||||
@@ -6217,32 +6217,45 @@ if (-r $path && open(my $in, "<", $path)) {
|
||||
|
||||
# If already known, nothing to do
|
||||
if ($set{$h}) {
|
||||
flock($lk, 8);
|
||||
flock($lk, LOCK_UN);
|
||||
close($lk);
|
||||
return;
|
||||
return 1;
|
||||
}
|
||||
|
||||
# Add and write out
|
||||
# Add
|
||||
$set{$h} = 1;
|
||||
|
||||
# Write out
|
||||
my $tmp = "$path.$$." . int(rand(1_000_000)) . ".tmp";
|
||||
if (open(my $out, ">", $tmp)) {
|
||||
print $out join(" ", sort keys %set), "\n";
|
||||
close $out;
|
||||
chmod 0700, $tmp;
|
||||
rename $tmp, $path or unlink $tmp;
|
||||
my $out;
|
||||
unless (open($out, '>', $tmp)) {
|
||||
flock($lk, LOCK_UN);
|
||||
close($lk);
|
||||
return 0;
|
||||
}
|
||||
my $wok = print {$out} join(" ", sort keys %set);
|
||||
my $cok = close $out;
|
||||
my $ok = $wok && $cok;
|
||||
|
||||
if ($ok) {
|
||||
$ok = rename $tmp, $path;
|
||||
$ok && chmod 0600, $path;
|
||||
}
|
||||
|
||||
flock($lk, 8);
|
||||
unlink $tmp if !$ok && -e $tmp;
|
||||
flock($lk, LOCK_UN);
|
||||
close($lk);
|
||||
|
||||
return $ok ? 1 : 0;
|
||||
}
|
||||
|
||||
# cleanup_wellknown()
|
||||
# Removes the well-known file and any temporary files
|
||||
sub cleanup_wellknown
|
||||
{
|
||||
my $path = $config{'wellknown'};
|
||||
for my $f ($path, "$path.lock") {
|
||||
next unless -e $f;
|
||||
unlink $f;
|
||||
}
|
||||
return unless $path && !-d $path;
|
||||
return unlink $path, "$path.lock", glob("$path.*.tmp");
|
||||
}
|
||||
|
||||
sub write_pid_file
|
||||
|
||||
Reference in New Issue
Block a user